【问题标题】:Why doesn't the UIImageView.center change when panning with UIPanGestureRecognizer (Swift)?使用 UIPanGestureRecognizer (Swift) 平移时,为什么 UIImageView.center 不改变?
【发布时间】:2014-12-28 04:47:17
【问题描述】:

当我使用“UIPanGestureRecognizer”移动“UIImageView”对象时,我注意到“center”属性没有改变。为什么会这样,我是不是做错了什么?代码如下:

func handlePanning1(recognizer: UIPanGestureRecognizer)
{
    var index: Int = recognizer.view!.tag - 1 // index in the arrays for this piece

    var newTranslation: CGPoint = recognizer.translationInView(pieces[index])

    recognizer.view?.transform = CGAffineTransformMakeTranslation(lastTranslations[index].x + newTranslation.x, lastTranslations[index].y + newTranslation.y)

    // THIS ALWAYS PRINTS OUT THE SAME WHILE I'M PANNING
    // AND IF I PAN MULTIPLE TIMES IN DIFFERENT DIRECTIONS (AKA IT NEVER CHANGES)
    print(Int(pieces[index].center.x))
    print("\n")

    if recognizer.state == UIGestureRecognizerState.Ended {
        lastTranslations[index].x += newTranslation.x
        lastTranslations[index].y += newTranslation.y
    }
}

【问题讨论】:

    标签: ios swift uiimageview uipangesturerecognizer panning


    【解决方案1】:

    您正在对视图应用变换,实际上并没有移动它。把它想象成它在某个地方,但是当它被渲染时,有一些说明可以改变它的显示方式。如果您希望视图具有不同的位置,则必须更改其中心属性而不是对其进行转换。

    【讨论】:

    • 哦,好吧,这很有意义。因此,在使用“UIPanGestureRecognizer”进行平移时,应用转换或实际移动它(通过更改“中心”)更好吗?
    • 取决于您要完成的工作。我没有研究哪个更有效,我会假设实际上移动它,但这只是一个假设。但是,如果您依赖其中心来更新位置,请继续更新中心。
    猜你喜欢
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多