2016-02-12 69 views
1

我发现这个代码是负责动画UIView,但不幸的代码不起作用,我不能找出原因(可能是一个老版本的swift)老swift代码不工作(动画UIView)

这是代码:

(这是根据创建者辅助函数)

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开发初学者,如果任何人都可以请解释代码为我

+0

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

回答

0

事实上,这moveView方法有几个问题,一个是它是为雨燕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 
    } 
} 

观察你的版本之间的差异和我明白了什么是过时/错了,我是如何解决它。如果你被卡住了,我会帮忙的。