2012-10-19 82 views
3

在這裏我的應用程序中使用了移動子視圖。如果它達到y = 150,我想在跳躍效果中做一個按鈕,我試過這個鏈接adding bounce effect to appearance of UIImageView它在水平方向工作,我想要一個垂直方向,這裏我的代碼,對uibuttons的跳躍效果

-(void)godown 
{ 
if (movingview.center.y < 150) movingview.center = CGPointMake(movingview.center.x, movingview.center.y +5); 
if(movingview.center.y ==150) 
{ 
    [UIView beginAnimations:@"bounce" context:nil]; 
    [UIView setAnimationRepeatCount:3]; 
    [UIView setAnimationRepeatAutoreverses:YES]; 
    CGAffineTransform transform = CGAffineTransformMakeScale(1.3,1.3); 
    bt1.transform = transform; 
    bt2.transform=transform; 
[UIView commitAnimations]; 
} 
} 

請幫我改成跳躍效果(垂直)?

回答

18

試試這個。你可以做一些改變,以適應你的需要,

 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
     anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
     anim.duration = 0.125; 
     anim.repeatCount = 1; 
     anim.autoreverses = YES; 
     anim.removedOnCompletion = YES; 
     anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]; 
     [senderView.layer addAnimation:anim forKey:nil]; 

希望這會有所幫助。

4

試試這個代碼..

- (IBAction)bounce { 
    CABasicAnimation *theAnimation; 
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];///use transform 
    theAnimation.duration=0.4; 
    theAnimation.repeatCount=2; 
    theAnimation.autoreverses=YES; 
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
    theAnimation.toValue=[NSNumber numberWithFloat:-20]; 
    [yourButton.layer addAnimation:theAnimation forKey:@"animateTranslation"];//animationkey  
} 

看到this鏈接全部答案

0

而迅速版

func jumpButtonAnimation(sender: UIButton) { 
     let animation = CABasicAnimation(keyPath: "transform.scale") 
     animation.toValue = NSNumber(float: 1.3) 
     animation.duration = 0.1 
     animation.repeatCount = 0 
     animation.autoreverses = true 
     sender.layer.addAnimation(animation, forKey: nil) 
    } 
0
UIView.animateWithDuration(0.1 , 
           animations: { 
           self.buttonName.transform = CGAffineTransformMakeScale(1.3, 1.3) 
     }, 
           completion: { finish in 
           UIView.animateWithDuration(0.1){ 
           self.buttonName.transform = CGAffineTransformIdentity 
           } 
    }) 
+0

你可以添加一些解釋你的答案?僅發佈代碼段不是一個好方法。參見[如何提問](http://stackoverflow.com/help/how-to-ask) –

1

斯威夫特3

在這裏,我發現了這個誘人的流行動畫有用的帖子。希望這是你想要的

enter image description here

@IBAction func btnCancel_click(_ sender: Any) 
{ 
    btnCancel.transform = CGAffineTransform(scaleX: 0.50, y: 0.50) 
    UIView.animate(withDuration: 2.0, 
        delay: 0, 
        usingSpringWithDamping: 0.2, 
        initialSpringVelocity: 6.0, 
        options: .allowUserInteraction, 
        animations: { [weak self] in 
        self?.btnCancel.transform = .identity 
     }, 
        completion: nil) 
} 

來源:http://evgenii.com/blog/spring-button-animation-with-swift/