使用 IF 语句设置 UIView 的位置

发布于 2024-10-11 02:16:09 字数 762 浏览 5 评论 0原文

在我创建的应用程序中,我有一个名为 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 >= ....”

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

In the application I am creating, I have a UIView named AdvancedView1. The user can drag the view around the screen. The user needs to drag the view to a certain position on the screen. If the view is dropped into a certain area, then the view would "snap" into a defined location. When trying to use IF AND statements, I am getting an error. Any suggestions?

Also "origin" is a CGPoint declared in the .h file.

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

I am getting the following error for "origin.x >= ...."

"Request for member "x" in something not a structure or union"

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

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

发布评论

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

评论(1

李不 2024-10-18 02:16:09

origin 变量范围仅限于第一个 if 块,在第二个 if 块中不可见。将其声明移至函数作用域:

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

origin variable scope is limited to the 1st if block and it is not visible in the 2nd one. Move its declaration to the function scope:

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