2017-09-05 193 views
1

我想知道如何製作一個UIView動畫,首先縮放到一定大小並旋轉該大小而不是旋轉原始大小?如何動畫UIView變換序列,如縮放然後在iOS中旋轉?

我已經嘗試了很多像UIView動畫完成UIView動畫,使用UIView animateKeyframes等等包裝UIView動畫的方法。

但是,我無法制作出完美的動畫,請給我提示或關鍵字進行搜索,謝謝!

+1

你能證明你做了什麼嗎? – Shebuka

+0

要搜索的關鍵詞:'ios微動畫uiview' – DonMag

+0

您是否瀏覽過此鏈接: - https://stackoverflow.com/questions/3703922/how-do-you-create-a-wiggle-animation-similar- to-iphone-deletion-animation https://stackoverflow.com/questions/929364/how-to-create-iphones-wobbling-icon-effect –

回答

0

希望您已經實現了縮放或搖動您的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]; 
1

這要根據您的要求工作(如何做一個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() 
     }) 

    } 


} 


結果

enter image description here

+0

這似乎是我想要的,我試圖像這樣構建,但是我可以連接三個或更多?我想縮放和搖動它。謝謝! –

+0

你能分享你的需求的GIF嗎? (由於網絡限制,Youtube視頻無法訪問),所以我可以幫助你更好。 – Krunal

+0

當然,我已經編輯它,非常感謝... –

2

你可以這樣做,

定義弧度度轉換成弧度

#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; 

} 
} 

而且結果是

enter image description here

您可以按照您的要求更改重複次數和弧度!

+0

它可以添加順序的規模變換?我無法使用縮放的大小來構建它。它會回到原來的大小,然後旋轉.. –

+0

嘗試添加像'_myView.transform = CGAffineTransformMakeScale(2,2);'! – Lion

+0

我試圖縮放然後旋轉使用你的方式,但它旋轉的原始大小... T T –