Swift,为特定CGPoint制作动画

时间:2016-07-18 13:38:15

标签: ios swift animation uiimageview cgpoint

我如何为编程创建的对象设置动画以转到特定的CGPoint。不使用CGAffineTransformMakeTranslation,因为这只是关于如何移动它,而不是移动它的确切位置。

我试过:translatesAutoresizingMaskIntoConstraints = true

但它没有任何区别。

我试过这个:

FATAL EXCEPTION: main
    Process: myapp.com.myapplication, PID: 30987
    com.firebase.client.FirebaseException: Failed to bounce to type
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:208)
        at myapp.com.myapplication.Login$2.onDataChange(Login.java:68)
        at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
        at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
        at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class myapp.com.myapplication.model.LoginData]: can not instantiate from JSON object (need to add/enable type information?)
        at [Source: java.io.StringReader@edca2f8; line: 1, column: 16]
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:432)
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:314)
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:206)
            ... 11 more

但是当在模拟器中运行时,绝对没有发生任何事情。

1 个答案:

答案 0 :(得分:2)

当您进行动画制作时,您的UI尚未渲染。把它放在viewDidAppear中。这对我有用:

class ViewController: UIViewController {

    @IBOutlet weak var centreCard3: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        // Sizes of centre cards

        let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5) / 2)
        let centralizeViewVerticleCard = screenHeight / 2

        UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: {
            self.centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, self.centreCard3.frame.size.width, self.centreCard3.frame.size.height)
            }, completion: nil)
    }
}

结果:

Result