15

在我的 xcode 项目中,我有一个 UIImage。我想让它从左到右稍微晃动(动画)。我写了这段代码,但是,它不起作用。

-(void)errorShake
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];



    [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-20 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(20 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];


    [UIView commitAnimations];
}

请你告诉我,我怎样才能让我的形象动摇?

4

4 回答 4

45

这是我用于该效果的代码。

 -(void)shakeView {

        CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
        [shake setDuration:0.1];
        [shake setRepeatCount:2];
        [shake setAutoreverses:YES];
        [shake setFromValue:[NSValue valueWithCGPoint:
                                 CGPointMake(lockImage.center.x - 5,lockImage.center.y)]];
        [shake setToValue:[NSValue valueWithCGPoint:
                               CGPointMake(lockImage.center.x + 5, lockImage.center.y)]];
        [lockImage.layer addAnimation:shake forKey:@"position"];
    }

如前所述Jere。确保包括导入:

#import <QuartzCore/QuartzCore.h>

您也可以将其作为类别添加到UIView.

于 2012-10-03T14:29:47.280 回答
11

brynbodayles 的回答也适用于 iOS8 下的快速摇动动画。

导入 QuartzCore:

func shakeView(){
    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 2
    shake.autoreverses = true

    var from_point:CGPoint = CGPointMake(LoginField.center.x - 5, LoginField.center.y)
    var from_value:NSValue = NSValue(CGPoint: from_point)

    var to_point:CGPoint = CGPointMake(LoginField.center.x + 5, LoginField.center.y)
    var to_value:NSValue = NSValue(CGPoint: to_point)

    shake.fromValue = from_value
    shake.toValue = to_value
    LoginField.layer.addAnimation(shake, forKey: "position")
}
于 2014-11-18T13:01:17.530 回答
1
func shakeView(_ view: UIView?) {
    let shake = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 2
    shake.autoreverses = true
    shake.fromValue = NSValue(cgPoint: CGPoint(x: (view?.center.x)! - 5, y: view?.center.y ?? 0.0))
    shake.toValue = NSValue(cgPoint: CGPoint(x: (view?.center.x)! + 5, y: view?.center.y ?? 0.0))
    view?.layer.add(shake, forKey: "position")
}
于 2018-04-02T07:30:16.100 回答
-1

来自电晕答案

在多个按钮和视图中提供摇动动画 SWIFT3

对于视图动画,只需将扩展 UIButton 更改为扩展 UIView

extension UIButton
{
    func shakeAnim ()
    {
        let shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
        shake.duration = 0.1
        shake.repeatCount = 2
        shake.autoreverses = true

        let from_point:CGPoint = CGPoint(x:self.center.x - 5,y: self.center.y)
        let from_value:NSValue = NSValue(cgPoint: from_point)

        let to_point:CGPoint = CGPoint(x:self.center.x + 5,y: self.center.y)
        let to_value:NSValue = NSValue(cgPoint: to_point)

        shake.fromValue = from_value
        shake.toValue = to_value
        self.layer.add(shake, forKey: "position")
   }
}
于 2017-02-20T12:41:23.217 回答