【问题标题】:Position of a UIView that's inside of the main UIView in a ViewController?ViewController 中主 UIView 内部的 UIView 的位置?
【发布时间】: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


【解决方案1】:

然后试试这个:

let frame = pointView.superview?.convertRect(pointView.frame, toView: nil)

有时视图需要他的 superView 来找到它的坐标

动画完成后放置

   UIView.animateWithDuration(1, animations: { () -> Void in
        //animation code
    }) { (success) -> Void in
        let frame = self.pointView.superview?.convertRect(self.pointView.frame, toView: nil)
    }

【讨论】:

  • 嗨,它不起作用 :( 它告诉我 PointView 参考超级视图的位置,但告诉我原点的位置,而不是移动的。我让 PointView 移动通过边界屏幕,我想知道按下按钮时的位置。谢谢!
  • 你能提供更多代码吗?你在哪里转换框架?当你的框架移动时,你必须放这条线
  • 是的,我用来移动视图的代码是这样的:stackoverflow.com/a/31011466/4496756
  • 我不这么认为。我必须把它放在哪里?
  • 在我说的完成块中,我编辑了我的答案给你看一个例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
相关资源
最近更新 更多