gpt4 book ai didi

iphone - 在 UIScrollView 中拖动 UIButtons?

转载 作者:行者123 更新时间:2023-12-01 19:25:07 24 4
gpt4 key购买 nike

我的主视图中有一个 ScrollView ,我的 ScrollView 中有三个 subview 。我的所有 subview 中都有 UIButtons。

现在,我想将这些按钮从一个 subview 拖动到另一个 subview (拖动按钮时, ScrollView 不应滚动)。

我该怎么做呢?

最佳答案

我不完全确定这个片段是否适用于这种特殊情况(UIScrollView 中的 UIControl),但我对 UIResponder 链的理解表明它应该:)

- (void)viewDidLoad { // or wherever you initialize your things
...
// Add swipe event to UIButton so it will capture swipe intents
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] init];
[panGR addTarget:self action:@selector(panEvent:)];
[button addGestureRecognizer:panGR];
[panGR release];
}

- (void)panEvent:(id)sender {
button.center = [sender locationInView:self.view];
}

如果这可行(现在无法测试,但在类似情况下它确实对我有用),那么您应该添加更多代码来处理拖放相关事件(可能禁用 UIScrollView 中的 Clip Subviews 选项,添加如果位置与目标的 CGRect 相交,则按钮指向新的 super View ,如果没有,则将按钮返回到原始位置,等等)。

那么,这些行中发生了什么?当您开始触摸 UIButton 时,订单不会到达 UIScrollView,因为该事件可以作为触摸事件(由 UIButton 处理)或作为平移事件(由 UIScrollView 处理)跟随。当您移动手指时,UIButton 的响应者会解除该事件,因为没有 Gesture Recognizer。如果手指移动,它知道如何进行。

但是,当您将手势识别器添加到实际上知道手指移动时该做什么的 UIButton 时,一切都不同了:UIButton 不会关闭事件,并且 UIScrollView 永远不会意识到有触摸在它上面移动。

我希望我的解释足够准确和易于理解。如果 a) 它不起作用或 b) 有不清楚的地方,请告诉我。

祝你好运 :)

关于iphone - 在 UIScrollView 中拖动 UIButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147187/

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