iOS中view視圖旋轉(zhuǎn)小計(jì)

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;
    }];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容