使用UIPanGestureRecognizer

时间:2015-12-18 06:07:10

标签: ios swift uiview resize uipangesturerecognizer

我有一条垂直线(UIView)我需要调整大小。我目前使用捏手势进行操作,但现在我需要使用滑动手势并从我滑动的一侧增加或减少高度。因此,例如,如果我从该行的TOP END向下滑动,则该行的大小必须仅从顶部开始减小。底部必须保持固定在原始位置。 我该怎么做?

我还需要能够移动图像,我已经使用UIPanGestureRecognizer实现了。

以下是我玩的内容: 我在垂直线的顶端有一个小UIView,在线的底端有一个UIView。我用这些作为标记。现在,我可以上下移动顶部标记。我需要将线调整到两个标记之间的距离,并将线的底端保持在与底部标记相同的位置。

这就是我所拥有的

func draggedViewForTopMarker(sender: UIPanGestureRecognizer) {
      //To move the top marker
        var translation = sender.translationInView(self.view)
        sender.view!.center = CGPointMake(sender.view!.center.x, sender.view!.center.y + translation.y)
        sender.setTranslation(CGPointZero, inView: self.view)
      //Find distance between the markers
        var distanceBetweenMarkers = (sender.view!.center.y + translation.y) - self.bottomMarker.center.y
        print(distanceBetweenMarkers)
        if (distanceBetweenMarkers < 0) {
            distanceBetweenMarkers = distanceBetweenMarkers*(-1)
        }
      //**TRYING** to resize the line to have the same height as the distance between the two markers AND make sure its positioned between the markers and the bottom end of the line is still in the same place as it was originally. 
        var newFrame = CGRectMake(sender.view!.center.x, distanceBetweenMarkers/2, vertical.frame.width, distanceBetweenMarkers)
        vertical.frame = newFrame
    }

我也对不同的方法持开放态度,或者解决我采用这种方法的问题!

您可以在App Store上的Photo Measures Lite查看,以便在我说“仅从一侧调整线路”时更好地理解我的意思。

非常感谢!

1 个答案:

答案 0 :(得分:0)

而不是制作新的框架,您可以将线条的y位置调整为与顶部标记相同,并且高度与两个标记之间的距离相同

func draggedViewForTopMarker(sender: UIPanGestureRecognizer) {
  //To move the top marker
    var translation = sender.translationInView(self.view)
    sender.view!.center = CGPointMake(sender.view!.center.x, sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
  //Find distance between the markers
    var distanceBetweenMarkers = self.topMarker.frame.origin.y - self.bottomMarker.frame.origin.y
vertical.frame.origin.y = self.topMarker.frame.origin.y
vertical.frame.size.height = distanceBetweenMarkers
}

但这仅在两个标记和垂直线是同一超视图的子视图时才有效