【问题标题】:Drag the UIView拖动 UIView
【发布时间】:2012-05-09 12:04:56
【问题描述】:

在某个框架内拖动一个 UIView,比如 (0, 0, 320, 190)。如何使用偏移量拖动 UIView。即当我触摸 UIView 时, UIView 不应该在手指下移动。

UIView 应该移动并在上面的框架内移动。我该怎么做?

请告诉我想法?

【问题讨论】:

  • touchesmoved :我覆盖它并将触摸点设置为中心
  • 方法 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  • 你能发布你的实现吗?
  • -(void)touchesMoved:(NSSet )touches withEvent:(UIEvent *)event{ UITouch touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:self]; CGFloat xDiff = touchPoint.x - mTouchStartPoint.x; CGFloat yDiff = touchPoint.y - mTouchStartPoint.y; self.center = CGPointMake(self.center.x + xDiff, self.center.y + yDiff); }
  • 请更新您的问题并格式化。不要在 cmets 中发布太多代码。

标签: iphone ios5 uiview draggable


【解决方案1】:

首先使用平移手势识别器来获取您的手指所在的位置,并计算 x 和 y 轴在您手指位置的 [gesture locationInView:parentView] 与子视图的框架位置之间的变化。将您的偏移量添加到该值。

使用刚刚计算的 x、y 变化,使用 UIView animateWithDuration 并在其中使用 CATransform3DTranslate 进行翻译。然后子视图将(带有动画)移动到您的手指+偏移量所在的位置。

要将其保留在父视图中,只需确保翻译不会导致子视图的框架超出父视图的范围,并剪辑父视图,以便子视图的任何部分超出父视图不会显示视图。

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 2017-09-28
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多