ios - 用手指 move UIView

标签 ios uiview move touchesbegan

我正在尝试 move 一个 UIView(我们称之为 viewA),它位于另一个 UIView(我们称之为 viewB)之上。 viewB 具有 iPad 屏幕的大小,而 View A 则小得多。

我设法 move 东西,但整个屏幕都 move (viewA + viewB),而不仅仅是 viewA。

这是我的 viewA 类代码:


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self->view]; if (CGRectContainsPoint(self.window.frame, touchLocation)) { dragging = YES; oldY = touchLocation.y; } }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self->view]; if (dragging) { CGRect frame = self.window.frame; frame.origin.y = self.window.frame.origin.y + touchLocation.y - oldY; self.window.frame = frame; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; }

我不明白的是,我在 viewA 类中实现了这些方法。 “ self ”应该关注这个观点吧?那么为什么当我的手指在屏幕上 move 时整个页面都会 move 呢?

最佳答案

您正在 move 整个窗口,其中包含所有 View ,您应该只 move 您想要的 View ,在您的情况下为 viewA。

关于ios - 用手指 move UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10411182/

相关文章:

ios - 使用选项卡栏 Controller 更改 View 时 UIView.animateWithDuration 完成得太早

ios - 使用 tableHeaderView 中的 UIView 创建 UISearchBar 行为

c++11 - std::move 与 lambda 中的 std::shared_ptr

git diff 重命名/move 和修改的文件但跳过重命名/move 和相同的文件

c# - 无法在 C# 中重命名目录,只能手动重命名

iphone - 如何将静态广告横幅添加到 UITableViewController 中?

iOS自定义后退按钮过渡不良

ios - 子类 UIView 上的 UIPanGestureRecognizer

ios - 如何在iOS11 App Store的应用程序页面上设置顶部图像

ios - 如何维护 UICollectionViewCell 中的按钮状态?