gpt4 book ai didi

ios - 如果检测到超出范围,则禁用平移手势

转载 作者:可可西里 更新时间:2023-11-01 04:42:16 24 4
gpt4 key购买 nike

我有一个 UIView 我正在尝试在屏幕上上下移动,但是我只想让它能够平移,这样当它处于正常位置 (0, 0) 时你就不能向下拖动 View

我试图检测识别器的中心何时不是 View 高度的一半,但是 View 是不可移动的,并且中心始终是高度的一半(在本例中为 230)。

有什么想法吗?

- (IBAction)panDetected:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
NSLog(@"\ncenter.y: %f\ntranslation.y: %f\n", recognizer.view.center.y, translation.y);
if (recognizer.view.center.y > ([[UIScreen mainScreen] bounds].size.height - 20)/2) {
return;
}
recognizer.view.center = CGPointMake(recognizer.view.center.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

最佳答案

不是手动计算点是否在 View 的边界内,而是使用 CGRectContainsPoint(rect, point)。这对我有用,我喜欢它,因为它更短且更易读:

func handlePan(pan: UIPanGestureRecognizer) {
switch pan.state {
case .Began:
if CGRectContainsPoint(self.pannableView.frame, pan.locationInView(self.pannableView)) {
// Gesture started inside the pannable view. Do your thing.
}
}

关于ios - 如果检测到超出范围,则禁用平移手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13881006/

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