如何实现“选择性” UIView 的 ClipToBounds,即剪辑一些子视图,但不剪辑其他子视图

发布于 2024-10-16 00:53:02 字数 501 浏览 2 评论 0原文

我想将视图的某些子视图剪切到视图的边界,而不是其他子视图。关于如何快速、轻松地实施这一点的任何想法。

对于上下文:我有一个 UIScrollView ,其中包含一组可以从视图中拖出的子视图。当将它们拖离视图时,我希望子视图不被剪切。否则我确实希望它们被剪掉。

  • 如果我在布局子视图之前执行 [view setClipToBounds: YES] 一切都会很好,直到我将子视图拖走。并且,如果在拖动开始时,我在touchesBegan:withEvent处执行[view setClipToBounds: NO],并在touchesEnded:withEvent:处执行[view setClipToBounds: YES],则滚动视图在拖动过程中会重绘,因此会出现先前剪切的子视图。
  • 我尝试过但没有成功的一种方法是,一旦拖动开始(在子视图的touchesBegan:withEvent中),就将子视图从滚动视图移动到滚动视图的超级视图。然而,当视图从一个视图移动到另一个视图时,它似乎失去了触摸的跟踪并且拖动结束。

I want to clip some of a view's subviews to the bounds of the view and not others. Any ideas as to how to set about implementing this quickly and easily.

For context: I have a UIScrollView with a set of subviews that can be dragged off the view. When dragging them off the view, I want the subview NOT TO BE CLIPPED. Otherwise I do want them clipped.

  • If I perform [view setClipToBounds: YES] before laying out the subviews all is well until I come to drag the subview off. And if, when dragging begins, I perform [view setClipToBounds: NO] at touchesBegan:withEvent and [view setClipToBounds: YES] at touchesEnded:withEvent:, then the scrollview redraws during the drag, so the previously clipped subviews appear.
  • One approach I have tried, but not got to work, is moving the subview from the scrollview to the scrollview's superview as soon as dragging begins (in the subview's touchesBegan:withEvent). However, when the view is moved from one view to another, it appears to lose track of touches and dragging is ends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-10-23 00:53:02

我通过以下方式解决了我的问题。当滚动视图的子视图接收到 TouchBegan:withEvent: 时,我将该子视图的 alpha 设置为 0.0。然后,我创建一个相同的子视图并将其添加到滚动视图的超级视图中。对于初始位置,我使用以下内容:(

CGPoint newOrigin = [self origin];
newOrigin.x = newOrigin.x + [[self superview] origin].x;
newOrigin.y = newOrigin.y + [[self superview] origin].y - [(UIScrollView*)[self superview] contentOffset].y;
[newSubview setOrigin: newOrigin];  // setOrigin is defined in a category on UIView - it does what you'd expect

注意 - 如果您不补偿滚动视图内容偏移量,您可能会得到令人费解的行为...)

我的 TouchMoved:withEvent 方法移动子视图(按照实现拖动的正常情况),然后touchesEnded:withEvent 从滚动视图的超级视图中删除额外的子视图,并将初始子视图的 alpha 设置回 1.0。因此,实际上,附加子视图依赖于不可见的子视图。

I solved my problem in the following way. When the subview of the scrollview receives a touchesBegan:withEvent: I set the alpha of that subview to 0.0. I then create an identical subview and add it to the scrollview's superview. For the initial position I use the following:

CGPoint newOrigin = [self origin];
newOrigin.x = newOrigin.x + [[self superview] origin].x;
newOrigin.y = newOrigin.y + [[self superview] origin].y - [(UIScrollView*)[self superview] contentOffset].y;
[newSubview setOrigin: newOrigin];  // setOrigin is defined in a category on UIView - it does what you'd expect

(Note - if you don't compensate for the scrollview content offset you can get puzzling behaviour...)

My touchesMoved:withEvent method moves the subview (as per normal for implementing dragging) and then touchesEnded:withEvent removes the extra subview from the scrollview's superview and sets the alpha of the initial subview back to 1.0. So, in effect, the additional subview, piggy backs on the invisible subview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文