【问题标题】:Animating a Circle when pressing UICollectionViewCell按下 UICollectionViewCell 时为圆设置动画
【发布时间】:2015-03-26 10:37:48
【问题描述】:

我试图在按下collectionViewCell 后实现动画。我有这个CABasicAnimation 电话:

CGRect frame = self.sharingAnimationView.bounds;

//// Subframes
CGRect group = CGRectMake(CGRectGetMinX(frame) + floor(CGRectGetWidth(frame) * 0.02041 + 0.5), CGRectGetMinY(frame) + floor(CGRectGetHeight(frame) * 0.02041 + 0.5), floor(CGRectGetWidth(frame) * 0.97959 + 0.5) - floor(CGRectGetWidth(frame) * 0.02041 + 0.5), floor(CGRectGetHeight(frame) * 0.97959 + 0.5) - floor(CGRectGetHeight(frame) * 0.02041 + 0.5));

//// Oval Drawing
CGRect ovalRect = CGRectMake(CGRectGetMinX(group) + floor(CGRectGetWidth(group) * 0.00000 + 0.5), CGRectGetMinY(group) + floor(CGRectGetHeight(group) * 0.00000 + 0.5), floor(CGRectGetWidth(group) * 1.00000 + 0.5) - floor(CGRectGetWidth(group) * 0.00000 + 0.5), floor(CGRectGetHeight(group) * 1.00000 + 0.5) - floor(CGRectGetHeight(group) * 0.00000 + 0.5));
UIBezierPath* ovalPath = UIBezierPath.bezierPath;
[ovalPath addArcWithCenter: CGPointMake(CGRectGetMidX(ovalRect), CGRectGetMidY(ovalRect)) radius: CGRectGetWidth(ovalRect) / 2 startAngle: -90 * M_PI/180 endAngle: 270 * M_PI/180 clockwise: YES];

CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.sharingAnimationView.bounds;
pathLayer.path = ovalPath.CGPath;
pathLayer.strokeColor = [[UIColor blackColor] CGColor];
pathLayer.fillColor = [[UIColor blackColor] CGColor];
pathLayer.lineWidth = 1.5;
pathLayer.lineJoin = kCALineJoinBevel;
[self.sharingAnimationView.layer addSublayer:pathLayer];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

这基本上在sharingAnimationView 中创建一个圆圈并将其着色为黑色。我正在从didSelectItemAtIndexPath 调用此方法,但似乎动画没有发生。

奇怪的是,如果我将动画调用移动到awakeFromNib,动画执行得很好。

有什么建议吗? 谢谢

【问题讨论】:

    标签: ios objective-c uicollectionviewcell cabasicanimation


    【解决方案1】:

    当您第一次运行一些 UI 更改(如标准 UIActivityIndi​​cator 或自定义动画)和接下来一些繁重的任务时,这是众所周知的行为。在这种情况下,RunLoop 不会执行 UI 更改,因为它希望完成之前的 UI 周期。如果您在 UI 线程中运行业务逻辑,那么您的活动指示器根本没有机会被处理。尝试首先在 didSelectItemAtIndexPath 中启动您的动画,然后将其余方法放在单独的方法中,并在运行动画后立即以最小的延迟调用它

    [self performSelector:@selector(mySeparateMethod) withObject:nil afterDelay:0.1];
    

    【讨论】:

    • 动画 coed 位于自定义 UICollectionViewCell 中。在 didSelectItemAtIndexPath 我告诉单元格开始动画
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多