【问题标题】:old swift code is not working (animating a UIView)旧的 swift 代码不起作用(为 UIView 设置动画)
【发布时间】:2016-02-12 11:35:56
【问题描述】:

我找到了这段代码,它负责为 UIView 设置动画,但不幸的是代码不起作用,我不知道原因(可能是 swift 的旧版本)

这是代码:

(根据创建者的说法,这是辅助函数)

func moveView(#view:UIView, toPoint destination:CGPoint, completion☹()->())?) {
 //Always animate on main thread
 dispatch_async(dispatch_get_main_queue(), { () Void in 
   //Use UIView animation API
   UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping:    
    0.6, initialSpringVelocity: 0.3, options:          
    UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> 
    Void in 
           //do actual move
           view.center = destination
    }, completion: { (complete) -> Void in
           //when animation completes, activate block if not nil
           if complete {
              if let c = completion {
                 c()
              }
           }
    })
 })
}

这是动画

//Create your face object (Just a UIImageView with a face as the image
var face = Face();
face.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
//find our trajectory points
var center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
var left = CGPointMake(center.x *-0.3, center.y)
var right = CGPointMake(center.x *2.2, center.y)
//place our view off screen
face.center = right
self.view.addSubview(face)
//move to center
moveView(view: face, toPoint: center) { () -> () in
  //Do your Pop
  face.pop()
  // Move to left
  moveView(view: face, toPoint: left, completion: { () -> () in
  }
}

我引用了代码的创建者

一般步骤:在屏幕的右边缘创建一个新面孔。制作 脸可见。将脸部移动到屏幕中间。弹出 face 用下一张脸开始这个过程。将第一个面移动到 新面孔一到中间就离开。

实际的幻灯片动画 再次,我们将在此处执行以下操作:移动 查看右侧屏幕外移到中心 Pop 移到左侧

要获得重复效果,只需在计时器上调用此方法

和一个总结:

UIView 的动画 API 非常强大。流行和运动 动画使用取决于此 API。如果你坚持尝试 创建动画,UIView 动画块通常是一个好地方 开始。

注意:如果有人可以为我解释代码,我是 IOS 开发的初学者

【问题讨论】:

  • 一切都很好。谢谢您,先生,我真的很感谢您的帮助。

标签: ios swift animation uiview


【解决方案1】:

这个moveView 方法确实有一些问题,一个是它是为 Swift 1 编写的(但也有一些拼写错误、错误字符和无用操作)。

这是固定版本:

func moveView(view view:UIView, toPoint destination: CGPoint, afterAnim: ()->()) {
 //Always animate on main thread
 dispatch_async(dispatch_get_main_queue(), { () -> Void in
   //Use UIView animation API
   UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping:
    0.6, initialSpringVelocity: 0.3, options:
    UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> Void in
           //do actual move
           view.center = destination
    }, completion: { (complete) -> Void in
           //if and when animation completes, callback
           if complete {
              afterAnim()
           }
    })
 })
}

你现在可以这样使用它:

moveView(view: face, toPoint: center) {
    //Do your Pop
    face.pop()
    // Move to left
    moveView(view: face, toPoint: left) {
        // Do stuff when the move is finished
    }
}

观察您的版本和我的版本之间的差异,以了解什么是过时/错误以及我如何修复它。如果您遇到困难,我会提供帮助。

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多