使用IF语句设置UIView的位置

时间:2011-01-10 09:32:58

标签: ios uiview ipad cgpoint

在我正在创建的应用程序中,我有一个名为AdvancedView1的UIView。用户可以在屏幕上拖动视图。用户需要将视图拖动到屏幕上的某个位置。如果视图被放入某个区域,则视图将“捕捉”到定义的位置。当尝试使用IF AND语句时,我收到一个错误。有什么建议吗?

此外,“origin”是在.h文件中声明的CGPoint。

- (void)pan:(UIPanGestureRecognizer *) gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) 
 {
     CGPoint origin = [gesture locationInView:[self superview]];
     [self bringSubviewToFront:[self superview]];
     [self setCenter:origin];            
}   
if (gesture.state == UIGestureRecognizerStateEnded) 
{
    if (origin.x >= 574.0 && origin.x <= 724.0 ) {
    self.frame = CGRectMake(574.0, 184.0, self.frame.size.width,   self.frame.size.height);
    }
}   
}

我收到“origin.x&gt; = ....”

的以下错误

“请求成员”x“在非结构或联合的东西中”

1 个答案:

答案 0 :(得分:1)

origin变量范围仅限于第1个if块,而在第2个块中不可见。将其声明移动到函数范围:

- (void)pan:(UIPanGestureRecognizer *) gesture
{
   CGPoint origin = [gesture locationInView:[self superview]];
   if (gesture.state == UIGestureRecognizerStateChanged) 
   {     
        [self bringSubviewToFront:[self superview]];
        [self setCenter:origin];            
   }   
   if (gesture.state == UIGestureRecognizerStateEnded) 
   {
       if (origin.x >= 574.0 && origin.x <= 724.0 ) {
          self.frame = CGRectMake(574.0, 184.0, self.frame.size.width,   self.frame.size.height);
       }
   }   
}