iOS中view视图旋转小计

view无限360旋转


- (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"];

}
如上代码,传入要旋转的view即可。

如果想要停止:


[self.playStatusImageView.layer removeAllAnimations];//停止动画

即可。

view无限360循环可暂停

- (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. 取消上次记录的停留时刻
    self.layer.timeOffset = 0.01;
    
     // 3. 取消上次设置的时间
    self.layer.beginTime = 0.0;
    
     // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
    CFTimeInterval timeWhenpause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    
     // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
    self.layer.beginTime = timeWhenpause;
    
}

设置旋转中心

/// 设定旋转中心并旋转
/// @param center 旋转中心
/// @param angle 旋转角度
- (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;
    }];
}

你可能感兴趣的:(iOS中view视图旋转小计)