ios - 使用 UIPanGestureRecognizer 仅从两端调整 UIView 的大小

标签 ios swift uiview resize uipangesturerecognizer

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

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

这是我一直在玩的东西: 我在垂直线的顶端有一个小的 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,以更好地理解我所说的“仅从一侧调整线条大小”的意思。

非常感谢!

最佳答案

无需创建新框架,您只需将线的 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
}

但这只有在两个标记和垂直线是同一个父 View 的 subview 时才有效

关于ios - 使用 UIPanGestureRecognizer 仅从两端调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349432/

相关文章:

ios - 隐藏选项卡的基于导航的应用程序

swift - 如何在 .childMoved 上获取新对象的索引

ios - iOS中多个UIView的UIView继承

ios - 当 UIView 从它的 superView 分离时如何得到通知?

ios - UIImage 未在 Xcode 6.3.1 Playground 中呈现

swift - Apple Watch 屏幕关闭时有没有办法记录 Action ?

ios - UIView:如何使宽度在另一个图像之前自动结束?

ios - 带有选项卡栏和导航栏的 ScrollView 自动布局

ios - 我应该使用 ImageView 还是 TextField?

ios - 在 UIView 中将一个 ImageView 和一个 UILabel 居中