簡體   English   中英

如何動畫UIView變換序列,如縮放然后在iOS中旋轉?

[英]How to animate UIView transform sequence like scaling then rotating in iOS?

我想知道如何制作UIView動畫,首先縮放尺寸並旋轉該尺寸而不是原始尺寸旋轉?

我已經嘗試了很多方法,比如在完成UIView動畫時包裝UIView動畫,使用UIView animateKeyframes等等。

但是,我無法完美地構建動畫,請給我提示或關鍵詞來搜索,謝謝!

這應該根據您的要求工作(如何制作首先縮放尺寸並旋轉該尺寸而不是在原始尺寸上旋轉的UIView動畫?)

@IBOutlet var scaleRotateImage: UIImageView!
func scaleNTransform() -> Void {

    scaleRotateImage.layer.cornerRadius = scaleRotateImage.frame.size.height/2.0
    scaleRotateImage.clipsToBounds = true

    let scaleTransform = CGAffineTransform(scaleX: 3.0, y: 3.0)  // Scale
    let rotateTransform = CGAffineTransform(rotationAngle: CGFloat.pi) // Rotation
    let hybridTransform = scaleTransform.concatenating(rotateTransform) // Both Scale + Rotation

    // Outer block of animation
    UIView.animate(withDuration: 5.0, animations: {
        self.scaleRotateImage.transform = scaleTransform
        self.view.layoutIfNeeded()
    }) { (isCompleted) in

        // Nested block of animation
        UIView.animate(withDuration: 5.0, animations: {
            self.scaleRotateImage.transform = hybridTransform
            self.view.layoutIfNeeded()
        })

    }


}


結果

在此輸入圖像描述

你可以做點什么,

定義弧度以將度數轉換為弧度

#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)

然后在你的viewDidAppear

-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

CGAffineTransform leftTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));

_myView.transform = leftTransform;  //Here `_myView` is the your view on which you want animations!

[UIView beginAnimations:@"youCanSetAnimationIdHere" context:(__bridge void * _Nullable)(_myView)];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationCompletedWithAnimationId:isCompleted:view:)];

_myView.transform = rightTransform;

[UIView commitAnimations];

}

你的animationCompletedWithAnimationId方法應該是這樣的,

- (void) animationCompletedWithAnimationId:(NSString *)animationID isCompleted:(NSNumber *)isCompleted view:(UIView *)view
{
if ([isCompleted boolValue]) {
    UIView *myView = view;
    myView.transform = CGAffineTransformIdentity;

}
}

結果是

在此輸入圖像描述

您可以根據您的要求更改重復次數和弧度!

希望您已經實現縮放或搖動您的UIButton ,此代碼將指導您使UIView動畫序列重復和自動反轉。

btn.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        // Your changes to the UI, eg. scale UIButton up 300 points
        btn.frame = frame2;
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        // Your changes to the UI, eg. rotate or shake UIButton
    }];
} completion:nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM