简体   繁体   English

如何在Swift中移动对象的位置?

[英]How do I move a position of an object in Swift?

I have an object I'm trying to animate to roll across the screen. 我有一个要制作动画的对象可以在屏幕上滚动。 I don't know how to change the position of the object and make it spin more than it is already. 我不知道如何更改对象的位置并使它旋转得比现在还多。 This is what I have so far: 这是我到目前为止的内容:

@IBOutlet weak var quarterImage: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    UIView.animateWithDuration(8.0, animations: {
        let angle = CGFloat(-4000)
        let rotate = CGAffineTransformMakeRotation(angle)
        self.quarterImage.transform = CGAffineTransformMakeRotation(angle)
    })

}

Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

I am using some bouncing animation of a smiley face image which jumps repeatedly. 我正在使用一些反复跳跃的笑脸图像的弹跳动画。 Here is the code so that you can have idea about changing position. 这是代码,以便您可以了解更改位置的想法。 For rotation I guess you need to play around with different PI values of angle to get smooth rotation. 对于旋转,我想您需要使用不同的角度PI值来获得平滑的旋转。

Swift 2 迅捷2

UIView.animateWithDuration(0.6, delay: 0.3, options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat, UIViewAnimationOptions.CurveEaseOut], animations:
    {
        self.m_smileyImgView?.transform.ty -= 30;

    },completion: nil);

Swift 3,4,5 斯威夫特3,4,5

UIView.animate(withDuration: 0.6, delay: 0.3, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat, UIView.AnimationOptions.curveEaseOut], animations:
    {
        self.m_smileyImgView?.transform.ty -= 30;

},completion: nil);

CGAffineTransformMakeRotation can be a little tricky CGAffineTransformMakeRotation可能有点棘手

First of all, it expects degrees in radians 首先,它期望以弧度为单位的度数

Second of all, values over 2PI doesn't matter (0 = 2PI = 6PI = 10000PI) 其次,超过2PI的值无关紧要(0 = 2PI = 6PI = 10000PI)

Here is an example function for rotating an image 360 degrees 这是将图像旋转360度的示例功能

func rotateImage(times times:Int) {
    if times == 0 { return }
    UIView.setAnimationCurve(.Linear)
    UIView.animateKeyframesWithDuration(1, delay: 0, options: .CalculationModeLinear, animations: {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
            self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2/3))
        })
        UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 1/3, animations: {
            self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 4/3))
        })
        UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
            self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2))
        })
    }) { (_) in
        self.rotateImage(times: times - 1)
    }
}

If you want to move the object while you rotate, you can change its position by adding this keyframe 如果要在旋转时移动对象,则可以通过添加此关键帧来更改其位置

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1.0, animations: { 
                self.imageView.center.x += 50
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM