使用 UIPanGesture 停止 UIView 离开容器?
Stop UIView leaving container with UIPanGesture?
我已经设法用平移手势阻止我的 UIView 超出容器,但我正在努力实现的是将其停止在容器的边缘。目前它一直到最后一个像素边缘,而不是可移动视图的边缘。
我尝试更改我认为可行的帧值,但无法产生正确的结果。不确定我有多接近或是否有更简单的方法?
我如何将它停在可移动视图的边缘而不是最后一个像素?
//Container is self.sliderContainer
//Green Block is sender.view which has a UIGesture applied
var location = sender.locationInView(self.view)
var newFrame = CGRectMake(0, 0, self.sliderContainer.frame.width, self.sliderContainer.frame.height)
let obstacleViewFrame = self.view.convertRect(self.sliderContainer.frame, fromView: self.sliderContainer.superview)
// Check if the touch is inside the obstacle view
if CGRectContainsPoint(obstacleViewFrame, location) {
sender.view!.center.x = sender.view!.center.x + translation.x
sender.setTranslation(CGPointZero, inView: self.view)
}
您似乎是根据触摸的坐标而不是 sender.view
的框架在幻灯片中移动发送方视图。
let senderViewFrame = self.view.convertRect(sender.view!.frame, fromView: sender.view!.superview)
let insetSenderViewFrame = CGRectInset(senderViewFrame, 0.0, 10.0 /*or whatever's needed*/)
let obstacleViewFrame = self.view.convertRect(self.sliderContainer.frame, fromView: self.sliderContainer.superview)
// Check if the sender view is inside the obstacle view
if CGRectContainsRect(obstacleViewFrame, insetSenderViewFrame) {
sender.view!.center.x = sender.view!.center.x + translation.x
sender.setTranslation(CGPointZero, inView: self.view)
}
我建议以最简单的方式来做,不限制手势,只确保矩形不会越界:
let movingView = sender.view!
let minCenterX = movingView.frame.size.width / 2
let maxCenterX = self.sliderContainer.frame.width - movingView.frame.size.width / 2
let newCenterX = movingView.center.x + translation.x
movingView.center.x = min(maxCenterX, max(minCenterX, newCenterX))
我已经设法用平移手势阻止我的 UIView 超出容器,但我正在努力实现的是将其停止在容器的边缘。目前它一直到最后一个像素边缘,而不是可移动视图的边缘。
我尝试更改我认为可行的帧值,但无法产生正确的结果。不确定我有多接近或是否有更简单的方法?
我如何将它停在可移动视图的边缘而不是最后一个像素?
//Container is self.sliderContainer
//Green Block is sender.view which has a UIGesture applied
var location = sender.locationInView(self.view)
var newFrame = CGRectMake(0, 0, self.sliderContainer.frame.width, self.sliderContainer.frame.height)
let obstacleViewFrame = self.view.convertRect(self.sliderContainer.frame, fromView: self.sliderContainer.superview)
// Check if the touch is inside the obstacle view
if CGRectContainsPoint(obstacleViewFrame, location) {
sender.view!.center.x = sender.view!.center.x + translation.x
sender.setTranslation(CGPointZero, inView: self.view)
}
您似乎是根据触摸的坐标而不是 sender.view
的框架在幻灯片中移动发送方视图。
let senderViewFrame = self.view.convertRect(sender.view!.frame, fromView: sender.view!.superview)
let insetSenderViewFrame = CGRectInset(senderViewFrame, 0.0, 10.0 /*or whatever's needed*/)
let obstacleViewFrame = self.view.convertRect(self.sliderContainer.frame, fromView: self.sliderContainer.superview)
// Check if the sender view is inside the obstacle view
if CGRectContainsRect(obstacleViewFrame, insetSenderViewFrame) {
sender.view!.center.x = sender.view!.center.x + translation.x
sender.setTranslation(CGPointZero, inView: self.view)
}
我建议以最简单的方式来做,不限制手势,只确保矩形不会越界:
let movingView = sender.view!
let minCenterX = movingView.frame.size.width / 2
let maxCenterX = self.sliderContainer.frame.width - movingView.frame.size.width / 2
let newCenterX = movingView.center.x + translation.x
movingView.center.x = min(maxCenterX, max(minCenterX, newCenterX))