其次,让绘制完的太阳随着拽动而旋转,效果图如下:

主要思想是在绘制完毕之后,通过affineTransform属性,让太阳图层旋转,代码如下:
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat height = -scrollView.contentOffset.y;
[CATransaction begin];
[CATransaction setDisableActions:YES];
if (height <= 100) {
//偏移值小于等于100的时候,不旋转
self.circleLayer.affineTransform = CGAffineTransformIdentity;
}else{
//偏移值小于等于100的时候,旋转
self.circleLayer.affineTransform = CGAffineTransformMakeRotation(-(M_PI / 720 * height - 100));
}
[CATransaction commit];
}
其中要注意的地方是需要禁止隐式动画,不然每次修改affineTransform都会自动带上隐式动画效果。
最后就是释放后,让tableView滚动到固定位置,然后让太阳持续旋转,屏蔽tableView的拖拽事件,主要用到的是CABaseAnimation,代码如下
-(void)p_rise {
self.tableView.scrollEnabled = NO;
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
anim.duration = 0.15;
anim.toValue = @(M_PI / 4.0);
anim.repeatCount = MAXFLOAT;
[self.circleLayer addAnimation:anim forKey:nil];
}
最后在经过一定时间后,移除图层动画,取消屏蔽tableView的拖拽事件,并且让tableView滚动到顶部,代码如下:
-(void)p_stop {
self.tableView.scrollEnabled = YES;
[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
[self.circleLayer removeAllAnimations];
}
源代码传送门
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。
注:相关教程知识阅读请移步到IOS开发频道。










