0

我有以下问题:
我有一个 UIImageView,我可以通过触摸和一个工具栏拖动它,我想靠近那个 Image View。这就是我目前正在做的事情:

- (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);
}

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

4

2 回答 2

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;
  }
}
于 2012-10-30T17:26:28.003 回答
0

您应该使用 UIGestureRecognizer,而不是 touchesMoved:。当你这样做时,图像视图上的手势识别器可以随意移动工具栏视图。

于 2012-10-30T17:27:37.653 回答