UIView 迅速改变它的位置
Posted
技术标签:
【中文标题】UIView 迅速改变它的位置【英文标题】:UIView changing its position in swift 【发布时间】:2014-09-05 21:42:54 【问题描述】:我如何使UIView
从其原始位置通过按一下按钮向上滑动,然后通过按一下按钮将其放回原位?使用 Swift 和 Xcode 6。
我目前已经尝试过:
@IBOutlet weak var DynView: UIView!
@IBAction func btnUp(sender: AnyObject)
【问题讨论】:
【参考方案1】:您必须实现一个动画来改变点击时DynView
的位置。这是一个例子:
@IBAction func btnUp(sender: AnyObject)
let xPosition = DynView.frame.origin.x
let yPosition = DynView.frame.origin.y - 20 // Slide Up - 20px
let width = DynView.frame.size.width
let height = DynView.frame.size.height
UIView.animateWithDuration(1.0, animations:
dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
)
【讨论】:
OK OK.. 我现在看到 Thiago.. 还有一件事它给了我一个错误:!CGRect 不能转换为 UIView 试试 dynView.frame = CGRectMake(xPosition, yPosition, height, width) //Derdida 感谢它消除了错误但是当我运行应用程序并单击我的向上按钮时它崩溃并且在我的应用程序委托中我收到“线程 1:信号 SIGABRT”以考虑我有此视图中的向上和向下按钮 没关系,伙计们,我明白了.. 这只是一个简单的语法错误.. 感谢 thiago 和 Derdida 所做的一切!!!!!! 嘿伙计们还有一件事.. 我们可以使用按钮上下移动视图,但现在我不希望视图的底部在我触摸向上时移动按钮。我希望它保持在默认位置.. 一般来说,我想要的只是移动视图的顶部..【参考方案2】:您好,如果您愿意,可以创建此扩展。 斯威夫特
创建文件 Extends.Swift 并添加此代码
/**
Extension UIView
by DaRk-_-D0G
*/
extension UIView
/**
Set x Position
:param: x CGFloat
by DaRk-_-D0G
*/
func setX(#x:CGFloat)
var frame:CGRect = self.frame
frame.origin.x = x
self.frame = frame
/**
Set y Position
:param: y CGFloat
by DaRk-_-D0G
*/
func setY(#y:CGFloat)
var frame:CGRect = self.frame
frame.origin.y = y
self.frame = frame
/**
Set Width
:param: width CGFloat
by DaRk-_-D0G
*/
func setWidth(#width:CGFloat)
var frame:CGRect = self.frame
frame.size.width = width
self.frame = frame
/**
Set Height
:param: height CGFloat
by DaRk-_-D0G
*/
func setHeight(#height:CGFloat)
var frame:CGRect = self.frame
frame.size.height = height
self.frame = frame
供使用(UIView 的继承)
inheritsOfUIView.setX(x: 100)
button.setX(x: 100)
view.setY(y: 100)
【讨论】:
【参考方案3】:我有点将两个投票最多的答案合二为一,并更新到 Swift 3。所以基本上创建了一个扩展,可以动画视图移动到不同的位置:
extension UIView
func slideX(x:CGFloat)
let yPosition = self.frame.origin.y
let height = self.frame.height
let width = self.frame.width
UIView.animate(withDuration: 1.0, animations:
self.frame = CGRect(x: x, y: yPosition, width: width, height: height)
)
【讨论】:
【参考方案4】: // MARK: - Properties
var bottomViewHeight: CGFloat = 200
var isViewHide = false
private let bottomView: UIView =
let view = UIView()
view.backgroundColor = .red
view.translatesAutoresizingMaskIntoConstraints = false
return view
()
private let showHideButton: UIButton =
let button = UIButton()
button.setTitle("Show / Hide", for: .normal)
button.setTitleColor(.black, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(showHideButtonTapped(_:)), for: .touchUpInside)
return button
()
// MARK: - Lifecycle
override func loadView()
super.loadView()
view.addSubview(bottomView)
NSLayoutConstraint.activate([
bottomView.heightAnchor.constraint(equalToConstant: bottomViewHeight),
bottomView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bottomView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
view.addSubview(showHideButton)
NSLayoutConstraint.activate([
showHideButton.widthAnchor.constraint(equalToConstant: 200),
showHideButton.heightAnchor.constraint(equalToConstant: 50),
showHideButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
showHideButton.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
])
override func viewDidLoad()
super.viewDidLoad()
showHideView(isShow: isViewHide)
// MARK: - Selectors
@objc func showHideButtonTapped(_ sender: UIButton)
print("? HIDE / SHOW BUTTON")
showHideView(isShow: isViewHide)
// MARK: - Functions
private func showHideView(isShow: Bool)
if isShow
UIView.animate(withDuration: 0.4)
self.bottomView.transform = CGAffineTransform(translationX: 0, y: self.bottomViewHeight)
else
UIView.animate(withDuration: 0.4)
self.bottomView.transform = CGAffineTransform(translationX: 0, y: 0)
isViewHide = !isViewHide
【讨论】:
以上是关于UIView 迅速改变它的位置的主要内容,如果未能解决你的问题,请参考以下文章
通过点击它来改变 UIView/UIControl 的屏幕位置
iOS:根据 View 位置动态改变 UILabel Alignment
我有一个带有不同/多个 UIView 的情节提要,并且我想要每个 UIView 都有一个 ViewController。迅速