2017-09-24 83 views
0

我编程另一个函数创建14个按钮和我添加的目标给他们,让他们每个动画一切,我已经目标时,我按任何按钮去的目标,我的问题如何搬回原来的位置使用UIView.Animate将后退按钮原来的位置UIView.Animate

正是这样的Gif:

Demo

代码:

var tileArrayXAxis: [Int] = [] // X-Axis of the my 14 buttons 
var tileArrayYAxis: [Int] = [] // Y-Axis of the my 14 buttons 
var tileArrayWidth: [Int] = [] // Width of the my 14 buttons 
var tileArrayHeight: [Int] = [] // Heigh of the my 14 buttons 

var targetArrayXAxis: [Int] = [] // X-Axis of the target 
var targetArrayYAxis: [Int] = [] // Y-Axis of the target 
var targetArrayWidth: [Int] = [] // Width of the target 
var targetArrayHeight: [Int] = [] // Heigh of the target 

var i : Int = 0 

func moveButton(sender: UIButton) { 

    var xS = Int() 
    var yS = Int() 
    var widthS = Int() 
    var heightS = Int() 

    if i < targetArrayXAxis.count && i < targetArrayYAxis.count && i < targetArrayWidth.count && i < targetArrayHeight.count { 

     xS = targetArrayXAxis[i] 
     yS = targetArrayYAxis[i] 
     widthS = targetArrayWidth[i] 
     heightS = targetArrayHeight[i] 
     yS -= widthS 

     let startS = CGPoint(x: xS, y: yS) 

     let sizeS = CGSize(width: widthS, height: heightS) 
     sender.frame = CGRect(x: startS.x, y: startS.y, width: sizeS.width, height: sizeS.width) 

    } 

    UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { [weak self] in 

     sender.transform = .identity 
     self?.i += 1   
    }) 
} 
+0

sender.transform = .identity不移回原来的位置? – Arrabidas92

+0

没有@ Arrabidas92 –

回答

0

设定您所设定的框架后,新的值不会移动的按钮变换身份因为它只会返回新的帧。

或者:你需要存储的旧框架中的变量,将其设置为一个新值之前:

let oldFrame : CGRect = sender.frame 
// Now set the frame to its new value 
sender.frame = .... 

然后在你的动画块:

sender.frame = oldFrame 

或者:DO不分配一个新的框架,但发送者分配一个平移变换它,然后将转换身份在动画块撤消转换并发送按钮回到它在哪里。

编辑:按照要求,这种简单的ViewController实现如下:

import UIKit 

类的ViewController:UIViewController的{

fileprivate var buttonOne : UIButton! 
fileprivate var buttonTwo : UIButton! 
fileprivate var buttonOneFrame : CGRect { 
    return CGRect(origin: CGPoint(x: view.bounds.width/2 - 200, y: 100), 
    size: CGSize(width: 150, height: 100)) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    buttonOne = { 
     let bO : UIButton = UIButton() 
     bO.backgroundColor = .red 
     bO.setTitle("Frame Method", for: .normal) 
     bO.addTarget(self, action: #selector(frameMethodMove(sender:)), for: .touchUpInside) 
     bO.frame = buttonOneFrame 
     return bO 
    }() 

    buttonTwo = { 
     let bT : UIButton = UIButton() 
     bT.backgroundColor = .blue 
     bT.setTitle("Transform Method", for: .normal) 
     bT.addTarget(self, action: #selector(transformMethodMove(sender:)), for: .touchUpInside) 
     bT.frame = CGRect(origin: CGPoint(x: view.bounds.width/2 + 50, y: 100), 
          size: CGSize(width: 150, height: 100)) 
     return bT 
    }() 

    view.addSubview(buttonOne) 
    view.addSubview(buttonTwo) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@objc fileprivate func frameMethodMove(sender: UIButton) -> Void { 

    let newFrame : CGRect = CGRect(origin: CGPoint(x: buttonOneFrame.origin.x, y: buttonOneFrame.origin.y + 500), 
            size: buttonOneFrame.size) 

    sender.frame = newFrame 

    sender.removeTarget(self, action: #selector(frameMethodMove(sender:)), for: .touchUpInside) 
    sender.addTarget(self, action: #selector(frameMethodMoveBack(sender:)), for: .touchUpInside) 
} 

@objc fileprivate func frameMethodMoveBack(sender: UIButton) -> Void { 

    UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: { 
     sender.frame = self.buttonOneFrame 
    }, completion: ({ _ in 

     sender.removeTarget(self, action: #selector(self.frameMethodMoveBack(sender:)), for: .touchUpInside) 
     sender.addTarget(self, action: #selector(self.frameMethodMove(sender:)), for: .touchUpInside) 
    })) 

} 

@objc fileprivate func transformMethodMove(sender: UIButton) -> Void { 

    sender.transform = CGAffineTransform.init(translationX: 0, y: 500) 

    sender.removeTarget(self, action: #selector(transformMethodMove(sender:)), for: .touchUpInside) 
    sender.addTarget(self, action: #selector(transformMethodMoveBack(sender:)), for: .touchUpInside) 
} 

@objc fileprivate func transformMethodMoveBack(sender: UIButton) -> Void { 

    UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: { 
     sender.transform = .identity 
    }, completion: ({ _ in 
     sender.removeTarget(self, action: #selector(self.transformMethodMoveBack(sender:)), for: .touchUpInside) 
     sender.addTarget(self, action: #selector(self.transformMethodMove(sender:)), for: .touchUpInside) 
    })) 
} 

}

+0

我想你解决,但是又回到了原来的位置自动我想回到原来的位置手动我的意思是按下它,我怎么能这样做? –

+0

请看看我的GIF,看看动画4个按钮目标和跳过位置并返回到原来的位置由按他们的@Sparky –

+0

@ user1.develop我明白你正在尝试做的 - 用点击返回按钮是微不足道。你可以通过将这些方法拆分为两个并替换按钮目标来实现。对于框架方法,您还需要将原始按钮位置保存为类属性(无论是UIButton还是子类);在我看来,变换方法可能更优雅。请参阅我的编辑。 – Sparky