我正在尝试制作一个带有 ImageView 的自定义切换按钮,单击时该按钮移动到容器的右侧,再次单击时移动到容器的左侧。为此我需要什么代码?
import Foundation
import UIKit
internal class container : UIViewController {
@IBOutlet weak var shape: UIImageView!
override internal func viewDidLoad() {
self.view.layer.cornerRadius = 10;
}
@IBAction func shapeMove(sender: AnyObject) {
}
}
shape 是应该从左到右移动的 ImageView 。
最佳答案
维护一个 bool 变量来维护状态。
var isImageLeftSide = true
@IBAction func shapeMove(sender: AnyObject) {
var customFrame = shape.frame
if isImageLeftSide {
customFrame.origin.x = customFrame.origin.x + 25 //assuming 25 is pixels by which your image should be shifted
}
else {
customFrame.origin.x = customFrame.origin.x - 25
}
shape.frame = customFrame
isImageLeftSide = ! isImageLeftSide
}
接下来您要做的就是检查状态并更改形状 View 的 x 坐标。如果在左侧则增加x坐标,如果在右侧则减少x坐标
关于ios - 如何在 Swift 中将 ImageView 从左向右移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35114594/