view無(wú)限360旋轉(zhuǎn)
- (void)rotateView:(UIImageView *)view{
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
rotationAnimation.duration = 1;
rotationAnimation.repeatCount = HUGE_VALF;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
如上代碼,傳入要旋轉(zhuǎn)的view即可。
如果想要停止:
[self.playStatusImageView.layer removeAllAnimations];//停止動(dòng)畫
即可忘闻。
view無(wú)限360循環(huán)可暫停
- (void)rotateView{
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
rotationAnimation.duration = 4;
rotationAnimation.repeatCount = HUGE_VALF;
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
self.isRotating = YES;
}
-(void)stop{
self.isRotating = NO;
CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.layer.speed = 0;
self.layer.timeOffset = pausedTime;
}
-(void)resume{
if (self.isRotating) {
return;
}
if (self.layer.timeOffset == 0) {
[self rotateView];
return;
}
self.isRotating = YES;
CFTimeInterval pausedTime = self.layer.timeOffset;
self.layer.speed = 1;
// 2. 取消上次記錄的停留時(shí)刻
self.layer.timeOffset = 0.01;
// 3. 取消上次設(shè)置的時(shí)間
self.layer.beginTime = 0.0;
// 4. 計(jì)算暫停的時(shí)間(這里也可以用CACurrentMediaTime()-pausedTime)
CFTimeInterval timeWhenpause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
// 5. 設(shè)置相對(duì)于父坐標(biāo)系的開(kāi)始時(shí)間(往后退timeSincePause)
self.layer.beginTime = timeWhenpause;
}
設(shè)置旋轉(zhuǎn)中心
/// 設(shè)定旋轉(zhuǎn)中心并旋轉(zhuǎn)
/// @param center 旋轉(zhuǎn)中心
/// @param angle 旋轉(zhuǎn)角度
- (void)transArmFormPoint:(CGPoint)center Angle:(CGFloat)angle{
CGPoint oldOrigin = self.frame.origin;
self.layer.anchorPoint = CGPointMake(center.x/self.frame.size.width, center.y/self.frame.size.height);
CGPoint newOrigin = self.frame.origin;
CGPoint transition;
transition.x = newOrigin.x - oldOrigin.x;
transition.y = newOrigin.y - oldOrigin.y;
self.center = CGPointMake (self.center.x - transition.x, self.center.y - transition.y);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformRotate(transform, M_PI*angle/180);
[UIView animateWithDuration:1 animations:^{
self.transform = transform;
}];
}