使用 IF 语句设置 UIView 的位置
在我创建的应用程序中,我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
origin
变量范围仅限于第一个 if 块,在第二个 if 块中不可见。将其声明移至函数作用域: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: