如何将可拖动对象保留在视图框架内

发布于 2024-12-08 23:07:48 字数 816 浏览 0 评论 0原文

我有一个可以在屏幕上拖动的按钮。我想知道是否有办法将按钮保留在视图框架内。

我使用此代码使按钮能够拖动:

UIPanGestureRecognizer *buttonPanRecognizer;
buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

CGPoint location = [recognizer locationInView:self.view];

if (CGRectContainsPoint([NewButton frame], location)) {              // NewButton
    NewButton.center = location;
} 
else if (CGRectContainsPoint([NewLabel frame], location)) {          // NewLabel
    NewLabel.center = location;
} }

我还希望能够将其他类型的对象保留在其中。

提前感谢:)

在此处输入图像描述

问题是可以将 UIButton 的部分内容拖到屏幕之外。

I have a button that can be dragged around on the screen. I was wondering if there is a way to keep the button inside the frame of the view.

I have used this code to make the button drag able:

UIPanGestureRecognizer *buttonPanRecognizer;
buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

CGPoint location = [recognizer locationInView:self.view];

if (CGRectContainsPoint([NewButton frame], location)) {              // NewButton
    NewButton.center = location;
} 
else if (CGRectContainsPoint([NewLabel frame], location)) {          // NewLabel
    NewLabel.center = location;
} }

I also want to be able to keep other kinds of objects inside.

Thansk in advance :)

enter image description here

The problem is that it is possible to drag parts of the UIButton outside the screen.

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

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

发布评论

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

评论(2

浅浅淡淡 2024-12-15 23:07:48

我必须同意你的问题不太清楚。带有标签的所有东西都应该是什么。一般来说,要将按钮保留在框架内,您已经拥有所需的一切:

UIPanGestureRecognizer* buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

    CGPoint location = [recognizer translationInView:self.view];

    if (CGRectContainsPoint(self.view.frame, location)) {
       NewButton.center = location;
    } 
}

I have to agree that your problem is not quite clear. What is all the stuff with the label supposed to to. Generally, to keep a button inside a frame, you have everything you need already there:

UIPanGestureRecognizer* buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

    CGPoint location = [recognizer translationInView:self.view];

    if (CGRectContainsPoint(self.view.frame, location)) {
       NewButton.center = location;
    } 
}
執念 2024-12-15 23:07:48

有什么问题吗?只需将按钮的原点保持在一个矩形内(frame.x,frame.y,frame.width-button.width,frame.height-button.height)。

What's the problem? Just keep the button's origin within a rect (frame.x,frame.y,frame.width - button.width,frame.height - button.height).

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