在UITableViewCell的imageView中,设置定时器CADisplayLink调用CGAffineTransformRotate改变transform时,
点击UItableViewCell时,会导致图标变形,代码如下:
@property(nonatomic,strong)CADisplayLink *link; -(CADisplayLink *)link { if (_link == nil) { //使用transform的方式旋转,每隔1/60秒移动一次,会改变imageView的frame _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; } return _link; } -(void)update { self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4 / 20); } -(void)startRotate { [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; } -(void)stopRotate { [self.link invalidate]; self.link = nil; [UIView animateWithDuration:1.0 animations:^{ self.imageView.transform = CGAffineTransformIdentity; }]; }
2、改用CABasicAnimation动画,解决了变形问题
-(void)stopRotate { [self.imageView.layer removeAnimationForKey:@"rotate-layer"]; } -(void)startRotate { //使用CABasicAnimation的方法,不会改变frame CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.rotation"; anim.duration = 2; anim.repeatCount = 1000; anim.toValue = @(M_PI * 2); anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; [self.imageView.layer addAnimation:anim forKey:@"rotate-layer"]; }
时间: 2024-10-10 05:41:19