ios - 如何在 Swift 中将 ImageView 从左向右移动

标签 ios swift

我正在尝试制作一个带有 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/

相关文章:

ios - 访问苹果的 iOS 铃声并显示它们

ios - NSDateFormatter 添加多余的时间

ios - NSURLSession 错误处理

ios - 当一个 Label adjustFontSizeToFitWidth 时,在 UITableViewCell 的 UILabels 上设置匹配的字体大小

ios - WKWebView 不允许点击网站中的按钮?

iphone - 当我添加 libZSDK_API.a 时,Restkit 的 -Objcflags会导致编译错误

iphone - 关于核心数据模型关系

ios - Swift 2.0 问题显示带有自定义声音文件的通知

ios - Swift:传递 C 函数 (SCNetworkReachabilityCallBack)

IOS WebRTC - 无法在屏幕上显示本地视频轨道