简体   繁体   English

如何在Swift中从左向右移动图像视图

[英]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.

相关问题 如何从右到左移动另一个视图上的一个视图 - how to move one view on another view from right to left 如何使用 Swift 在 iOS 中从右到左呈现视图控制器 - How to present view controller from right to left in iOS using Swift 如何使用Swift从右到左和从左到右制作自定义视图的动画 - How to animate a Custom View Right to Left and Left to Right using Swift 如何触摸并按住屏幕左侧和右侧来移动图像? -迅捷 - How would I move my image with a touch and hold on the left and right side of the screen? - Swift 当我们向左或向右移动 slider 时,是否有任何方法可以在 swift ios 中实现前后视图功能以进行并排图像比较? - Is there any way to implement before and after view functionality in swift ios for side by side image comparison as we move the slider left or right? UIImageView从左到右查看图像 - UIImageView view Image from Left to Right 小数点从右向左移动 iOS Swift 5.3 - Move numbers from right to left of a decimal point in iOS Swift 5.3 连续从左向右移动图像 - Move image left to right continuously iOS:自动版式将视图从左至右移动 - iOS: autolayout move view from left to top right 如何使视图跟随我的手指移动? 方向是从左到右 - How to make a view follow my finger to move? Direction is from left to right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM