【发布时间】:2015-06-24 21:13:51
【问题描述】:
我正在用 Swift 做一个应用程序项目,我有一个 UIView 在主视图中移动(默认情况下它在 ViewController 中的那个)。
我想知道按下按钮时 UIView 的确切位置(X 和 Y)。
我尝试过这样的事情,但对我不起作用:
let X = pointView.frame.origin.x + pointView.frame.origin.x;
let Y = pointView.frame.origin.y + pointView.frame.origin.y;
或
let frame = self.view.convertRect(self.view.frame, fromView:pointView)
我现在的代码:
@IBAction func pressButton(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
UIView.animateKeyframesWithDuration(4, delay: 0, options: UIViewKeyframeAnimationOptions.Repeat, animations: { () -> Void in
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1.5/4.0, animations: { () -> Void in
//1
self.pointView.center = CGPointMake(2, screenHeight-2)
//2
})
UIView.addKeyframeWithRelativeStartTime(1.5/4, relativeDuration: 1/4, animations: { () -> Void in
self.pointView.center = CGPointMake(screenWidth-2, screenHeight-2)
})
UIView.addKeyframeWithRelativeStartTime(2.5/4, relativeDuration: 1.5/4.0, animations: { () -> Void in
self.pointView.center = CGPointMake(screenWidth-2, 1)
})
//3
}, completion: nil)
//4
}
【问题讨论】:
-
pointView 是什么?如果 self.view 正在移动,为什么不直接读取他的 frame 属性?
-
Self.view 我认为这是主视图,而 PointView 是我想知道按下按钮时它在哪里的视图,它是正在移动的视图。
标签: ios swift cocoa-touch uiview uiviewcontroller