简体   繁体   English

旧的swift代码无法正常工作(动画UIView)

[英]old swift code is not working (animating a UIView)

i have found this code which is responsible for animating a UIView but unfortunately the code does not work and i can not figure the reason (maybe an older version of swift) 我已经找到了负责对UIView进行动画处理的代码,但是不幸的是,该代码无法正常工作,我无法弄清原因(也许是swift的旧版本)

this is the code : 这是代码:

(this is helper function according to the creator) (根据创建者,这是辅助功能)

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()
              }
           }
    })
 })
}

and this is the animation 这是动画

//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
  }
}

and i quote from the creator of the code 我引用代码的创建者

General Steps: Create a new face on the right edge of the screen. 常规步骤:在屏幕右侧创建一个新面孔。 Make the face visible. 使脸部可见。 Move the face to the middle of the screen. 将脸部移动到屏幕中间。 Pop the face Start the process with the next face. 弹出脸部从下一张脸部开始该过程。 Move the first face to the left as soon as the new face gets to the middle. 新面孔到达中间后,将第一张面孔向左移动。

Actual slide animation Once again, we will do the following here: Move view off screen on the right Move to center Pop Move to left 实际的幻灯片动画再一次,我们将在此处执行以下操作:将视图移出右侧屏幕外移至中心Pop移至左侧

To get the repeating effect, just call this method on a timer 要获得重复效果,只需在计时器上调用此方法

and a summary : 和摘要:

UIView's animation API is very powerful. UIView的动画API非常强大。 Both the pop and movement animations use depend on this API. 流行动画和动作动画都取决于此API。 If you're stuck with trying to create an animation, UIView animation block is usually a good place to start. 如果您不愿意尝试创建动画,那么UIView动画块通常是一个不错的起点。

NOTE : im a beginner in IOS development if anyone can please explain the code for me 注意:我是IOS开发的初学者,如果有人可以为我解释代码

Indeed this moveView method had a few issues, one being it was written for Swift 1 (but there were also some typos, faulty characters and useless operations). 确实,这个moveView方法有几个问题,一个是为Swift 1编写的(但也有一些错别字,错误的字符和无用的操作)。

Here's the fixed version: 这是固定版本:

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()
           }
    })
 })
}

You can use it like this now: 您现在可以像这样使用它:

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

Observe the differences between your version and mine to understand what was obsolete/wrong and how I fixed it. 观察您的版本和我的版本之间的差异,以了解过时/错误以及我如何修复它。 I'll help if you're stuck. 如果您遇到困难,我会帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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