gpt4 book ai didi

ios - 可拖动的 UIView 在添加到 UIScrollView 后停止发布 touchesBegan

转载 作者:可可西里 更新时间:2023-11-01 05:21:31 25 4
gpt4 key购买 nike

在 Xcode 5.1 中我创建了 a simple test app对于 iPhone:

app screenshot

结构是:scrollView -> contentView -> imageView -> image 1000 x 1000 在上面。

在单 View 应用程序的底部,我有七个可拖动的自定义 UIView

拖动在Tile.m中实现使用 touchesXXXX 方法。

我的问题是:一旦我在我的 ViewController.m 中向 contentView 添加了一个可拖动的图 block 。文件 - 我不能再拖动它了:

- (void) handleTileMoved:(NSNotification*)notification {
Tile* tile = (Tile*)notification.object;
//return;

if (tile.superview != _scrollView && CGRectIntersectsRect(tile.frame, _scrollView.frame)) {
[tile removeFromSuperview];
[_contentView addSubview:tile];
[_contentView bringSubviewToFront:tile];
}
}

touchesBegan 不再为 Tile 调用,就好像 scrollView 会屏蔽该事件一样。

I've searched around并且有人建议使用以下方法扩展 UIScrollView 类(在我的自定义 GameBoard.m 中):

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView* result = [super hitTest:point withEvent:event];

NSLog(@"%s: %hhd", __PRETTY_FUNCTION__,
[result.superview isKindOfClass:[Tile class]]);

self.scrollEnabled = ![result.superview isKindOfClass:[Tile class]];
return result;
}

不幸的是,这没有帮助并在调试器中打印 0

最佳答案

部分原因是因为用户交互在内容 View 中被禁用。但是,启用用户交互会禁用滚动,因为 View 会捕获所有触摸。所以这是解决方案。在 Storyboard 中启用用户交互,但像这样子类化内容 View :

@interface LNContentView : UIView

@end

@implementation LNContentView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView* result = [super hitTest:point withEvent:event];

return result == self ? nil : result;
}

@end

这样,只有当接受 View 不是self,即内容 View 时, HitTest 才会通过。

这是我的 promise : https://github.com/LeoNatan/ios-newbie

关于ios - 可拖动的 UIView 在添加到 UIScrollView 后停止发布 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440327/

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