gpt4 book ai didi

ios - 使用 UIView.Animate 将后退按钮移至原始位置

转载 作者:行者123 更新时间:2023-11-30 12:08:24 26 4
gpt4 key购买 nike

我正在另一个函数中以编程方式创建 14 个按钮,我向它们添加了目标并让它们全部动画化,并且当我按下任何按钮时我都有目标转到目标,我的问题如何移回使用原始位置 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
})
}

最佳答案

将框架设置为新值后将变换设置为标识根本不会移动按钮,因为它只会返回新框架。

要么:您需要将旧帧存储在变量中,然后再将其设置为新值:

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

然后在你的动画 block 中:

sender.frame = oldFrame

或者:不向发送者分配新帧,而是为其分配平移变换,然后在动画 block 中将变换设置为identity以撤消变换并将按钮发送回原来的位置。

编辑:根据要求,一个简单的 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)
}))
}

}

关于ios - 使用 UIView.Animate 将后退按钮移至原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com