简体   繁体   中英

Swift animateWithDuration()

I want to change the imageView's y in an animation and change the text of the label. But when I do that. The ImageView goes up 100 and then does the animation from -100 to 0. But I want it do go down 100.

If I don't change the text of the label, the animation works perfectly.

    label.text = "test"

    UIView.animateWithDuration(0.5, animations: {

        self.imageView.frame.origin.y += 100

    })

Also if I do that, it only changes the text and doesn't move the imageView.

    label.text = "test"
    imageView.frame.origin.y += 100

You are using AutoLayout. You cannot change the frame directly when using auto layout.

You have to set up the constraints and then animate the change in constraints to animate the views.

Also, you can't do blah.frame.origin.y += 100 IIRC the origin on CGrect is read only.

You can do...

Blah.frame = CGRectOffset(blah.frame, 0, 100)

But again that won't work with auto layout.

Edit

The origin is settable in swift. Thanks jrturton:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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