gpt4 book ai didi

iOS 使 View 智能地跟随另一个 View

转载 作者:行者123 更新时间:2023-11-29 04:19:32 24 4
gpt4 key购买 nike

我有以下问题:
我有一个可以通过触摸拖动的 UIImageView 和一个工具栏,我希望它靠近该 ImageView 。这就是我现在正在做的事情:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//motion here;self.tool is toolbar View

CGFloat a=self.tool.frame.size.width;
CGFloat b=self.tool.frame.size.height;
self.tool.frame=CGRectMake(self.frame.origin.x+self.frame.size.width/2+50, self.frame.origin.y+self.frame.size.height/2+50, a, b);
}

它工作正常,但有时工具栏会移到屏幕之外。也许有一种简单的方法可以跟踪我是否在外面并将工具栏移动到另一个点?

最佳答案

你可以这样检查:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//motion here;self.tool is toolbar View

CGFloat a = self.tool.frame.size.width;
CGFloat b = self.tool.frame.size.height;
CGRect newFrame = CGRectMake(self.frame.origin.x+self.frame.size.width/2+50,
self.frame.origin.y+self.frame.size.height/2+50,
a, b);

// only set frame, if it is still in the bounds of self.superview
if(CGRectContainsRect(self.superview.frame, newFrame)) {
self.tool.frame = newFrame;
}
}

关于iOS 使 View 智能地跟随另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144276/

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