作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究一种座位安排应用程序。该应用程序允许管理员通过单击并移动每个座位来安排座位,然后使用每个座位的框架尺寸保存座位安排。我尝试在自定义 View 上加载 5 个 UIImageView 并移动 ImageView ,但是只有一个 ImageView 被移动。其余的 ImageView 没有移动。你能帮我解决这个问题吗?
- (void) addSeatsToTheView {
for (int i=0; i<5; i++) {
self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
self.hotelImage.image = [UIImage imageNamed:@"DiningIcon"];
self.hotelImage.userInteractionEnabled = YES;
[self.hotelImage setUserInteractionEnabled:YES];
self.hotelImage.tag = i;
[self.hotelView addSubview:self.hotelImage];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
管理员安排好座位后,我们必须保存所有可见的 uiimageview 帧大小。谢谢。
编辑:
我尝试使用 UIPanGestureRecognizer,但是只有一个 ImageView 移动了。不知道哪里做错了?你能帮我么?谢谢。
- (void) addSeatsToTheView {
for (int i=0; i<5; i++) {
self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
self.hotelImage.image = [UIImage imageNamed:@"DiningIcon"];
self.hotelImage.userInteractionEnabled = YES;
[self.hotelImage setUserInteractionEnabled:YES];
self.hotelImage.tag = i;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerMethod:)];
[self.hotelImage addGestureRecognizer:panGestureRecognizer];
[self.hotelView addSubview:self.hotelImage];
}
}
- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
if (recogniser.state == UIGestureRecognizerStateBegan || recogniser.state == UIGestureRecognizerStateChanged)
{
CGPoint touchLocation = [recogniser locationInView:self.hotelView];
self.hotelImage.frame = CGRectMake(touchLocation.x, touchLocation.y, 50, 50);
}
}
最佳答案
不要为此使用touches
方法;你会发疯的。使每个 ImageView 都具有用户交互性,并为其提供一个 UIPanGestureRecognizer。平移手势识别器的 Action 处理程序有标准代码用于跟随手指,即使 View 可拖动。
一旦你这样做了,一切都源于你对 self.hotelImage
的滥用。完全摆脱它。在您的 for
循环中,只需使用局部变量 UIImageView* hotelImage = ...
。在手势识别器方法中,使用 [recogniser view]
来指代 this 手势识别器所附加的 ImageView 。之后一切都会自行解决!
关于ios - 如何顺利移动 UIImageViews 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150522/
我是一名优秀的程序员,十分优秀!