[英]How to move image view from left to right in Swift
Im trying to make a custom switch button with an image view that moves to the right of its container when clicked and to the left of the container when clicked again. 我试图用图像视图制作一个自定义开关按钮,该图像视图在单击时会移至其容器的右侧,而在再次单击时会移至该容器的左侧。 What code do I need for this? 我需要什么代码?
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 is the image view that should be moved left to right. shape是应从左向右移动的图像视图。
Maintain a bool variable for maintaining the state. 维护布尔变量以保持状态。
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
}
Next all you have to do this check the state and change x coordinate of your shape view. 接下来,您要做的所有事情就是检查状态并更改形状视图的x坐标。 If it is to left side increase the x coordinate and if it is to right side decrease x coordinate 如果在左侧,请增加x坐标;如果在右侧,请减少x坐标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.