gpt4 book ai didi

ios - 如何顺利移动 UIImageViews 数组?

转载 作者:行者123 更新时间:2023-11-28 19:32:43 25 4
gpt4 key购买 nike

我正在研究一种座位安排应用程序。该应用程序允许管理员通过单击并移动每个座位来安排座位,然后使用每个座位的框架尺寸保存座位安排。我尝试在自定义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com