旧的 swift 代码不起作用(为 UIView 制作动画)



我找到了这段代码,它负责对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
  }
}

我引用代码的创建者的话

常规步骤:在屏幕右边缘创建新面孔。做 脸可见。将面部移动到屏幕中间。弹出 脸 从下一张脸开始该过程。将第一个面移动到 新面孔一到中间就离开了。

实际幻灯片动画 再次,我们将在此处执行以下操作: 移动 右侧屏幕外视图 移至居中 弹出式 向左移动

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

和摘要:

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

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

事实上,

这个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
    }
}

观察您的版本与我的版本之间的差异,以了解过时/错误的内容以及我如何修复它。如果你遇到困难,我会帮忙的。