gpt4 book ai didi

ios - 如何将可拖动的 UIView 限制在特定范围内

转载 作者:行者123 更新时间:2023-11-28 08:36:19 62 4
gpt4 key购买 nike

我有一个 UIImageView,我通过名为 DraggableImageView2 的自定义类将其设置为 Draggable。然后该类在带有图像的 UIViewController 中实例化,并且可以在 UIView 中拖动。问题是我想限制 UIImageView 在拖动时不要超过特定边界。这是我当前的代码:

import UIKit

class DraggableImageView2: UIImageView, UIGestureRecognizerDelegate {

var dragStartPositionRelativeToCenter : CGPoint?

var dragGesture: UIGestureRecognizer!
var zoomGesture: UIGestureRecognizer!

var lastKnownCenterX: CGFloat = 0.0
var lastKnownCenterY: CGFloat = 0.0

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

self.initializeGestures()

}


override init(image: UIImage?) {

super.init(image: image)

self.initializeGestures()

}

override init(frame: CGRect) {

super.init(frame: frame)

self.initializeGestures()

}

func initializeGestures() {

self.userInteractionEnabled = true
self.multipleTouchEnabled = true

dragGesture = UIPanGestureRecognizer(target: self, action: #selector(DraggableImageView.handlePan(_:)))

self.addGestureRecognizer(dragGesture)

}

func handlePan(recognizer: UIPanGestureRecognizer!) {

Scripts.log("PAN >>> MOVE ACTIVATED")

if recognizer.state == UIGestureRecognizerState.Began {

let locationInView = recognizer.locationInView(superview)
dragStartPositionRelativeToCenter = CGPoint(x: locationInView.x - center.x, y: locationInView.y - center.y)

return

}

if recognizer.state == UIGestureRecognizerState.Ended {

dragStartPositionRelativeToCenter = nil

return
}

let locationInView = recognizer.locationInView(superview)

Scripts.log("PAN LOCATION >>> X = \(self.frame.origin.x) | Y = \(self.frame.origin.y) | LX = \(locationInView.x) | LY = \(locationInView.y) | DX = \(self.dragStartPositionRelativeToCenter!.x) | DY = \(self.dragStartPositionRelativeToCenter!.y)")

let xDragMin: CGFloat = 1.0
let yDragMin: CGFloat = 1.0
let xDragMax: CGFloat = kDEVICE_WIDTH - self.frame.size.width - 1.0
let yDragMax: CGFloat = kDEVICE_WIDTH - self.frame.size.height - 1.0

var newCenterX: CGFloat = locationInView.x - self.dragStartPositionRelativeToCenter!.x
var newCenterY: CGFloat = locationInView.y - self.dragStartPositionRelativeToCenter!.y

if self.frame.origin.x < xDragMin {

Scripts.log("PAN LOCATION >>> X MIN PAST BOUNDS")

newCenterX = lastKnownCenterX

}

if self.frame.origin.y < yDragMin {

Scripts.log("PAN LOCATION >>> Y MIN PAST BOUNDS")

newCenterY = lastKnownCenterY

}

if self.frame.origin.x > xDragMax {

Scripts.log("PAN LOCATION >>> X MAX PAST BOUNDS")

}

if self.frame.origin.y > yDragMax {

Scripts.log("PAN LOCATION >>> Y MAX PAST BOUNDS")

}


UIView.animateWithDuration(0.1) {
self.center = CGPoint(x: newCenterX,
y: newCenterY)
}

lastKnownCenterX = newCenterX
lastKnownCenterY = newCenterY

}


func handlePinch(recognizer: UIPinchGestureRecognizer!) {

Scripts.log("PINCH >>> ZOOM ACTIVATED")

//self.bringSubviewToFront(recognizer.view!)

recognizer.view?.transform = CGAffineTransformScale((recognizer.view?.transform)!, recognizer.scale, recognizer.scale)
recognizer.scale = 1

}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

Scripts.log("shouldRecognizeSimultaneouslyWithGestureRecognizer WAS CALLED")

return true

}

}

我才开始处理 xDragMinyDragMin 部分。当它小于 xDragMin 时,一切都可以停止它,但是由于一些奇怪的原因,问题变成了,当你试图拖出它时,它被卡住在那里X 点。 yDragMin也是一样,如果到达最小值,它应该停止,但不能拖出 Y 点。

最佳答案

所以经过更多的挖掘和尝试,我似乎已经能够解决我自己的问题,至少对于 X Bounds Min。这是我所做的:

(1) 添加了这些新变量

var isWithinXBounds: Bool = true
var lastKnownLX: CGFloat = 0.0

(2) 需要微调以“if self.frame.origin.x < xDragMin”开头的逻辑。在达到 X 边界之前,这种情况非常好。一旦完成,self.frame.origin.x 将永远不会改变 (DUH) 因为现在它的位置被锁定了。我需要另一个条件来检查一旦边界被击中即锁定,检查用户触摸是否在 +ve 方向移动。如果是,则将 BOOL 值设置回 TRUE,这将解锁 x 位置

  if self.frame.origin.x < xDragMin {

Scripts.log("PAN LOCATION >>> X MIN PAST BOUNDS")

self.isWithinXBounds = false

//If TOUCH still moving in negative direction
if locationInView.x - lastKnownLX < 0 {

newCenterX = lastKnownCenterX

}
else {

self.isWithinXBounds = true

}

}

(3) 下面是根据 BOOL 值解锁 UIImageView 的条件:

if isWithinXBounds {

lastKnownCenterX = newCenterX
lastKnownLX = locationInView.x

}

关于ios - 如何将可拖动的 UIView 限制在特定范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37594757/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com