我有以下问题:
我有一个UIImageView,我可以通过触摸和工具栏拖动,我想要靠近图像视图。这就是我现在正在做的事情:
- (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);
}
它工作正常但有时工具栏移动到屏幕之外。也许有简单的方法来跟踪我是否在外面并将工具栏移动到另一个点?
答案 0 :(得分:1)
您可以这样检查:
- (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;
}
}
答案 1 :(得分:0)
你应该使用UIGestureRecognizer,而不是touchesMoved:。当你这样做时,图像视图上的手势识别器可以随意移动工具栏视图。