gpt4 book ai didi

cocoa - 在屏幕上拖动 UIImageView?

转载 作者:行者123 更新时间:2023-12-03 17:41:57 26 4
gpt4 key购买 nike

我的应用程序中有以下代码,每次我都可以复制和粘贴它,但我宁愿让它在其他区域也可以重用。它允许我在屏幕上拖动 UIImageViews,但我不喜欢这样一个事实:它很容易因为最轻微的拼写错误而出错!

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];




if (touch.view == first) {
first.center = location;

if (((first.center.x >= 100) && (first.center.y >= 100) &&
(first.center.x <= 150) && (first.center.y <= 150))) {

first.center = CGPointMake(125, 125);
[first setUserInteractionEnabled:NO];

theCentre = [[NSUserDefaults standardUserDefaults] init];

NSString *centre = NSStringFromCGPoint(first.center);

[theCentre setObject:centre forKey:@"centre"];

// NSLog(@"%@", [theCentre objectForKey:@"centre"]);
[theCentre synchronize];

}

}
if (touch.view == second) {
second.center = location;

if (((second.center.x >= 150) && (second.center.y >= 150) &&
(second.center.x <= 180) && (second.center.y <= 180))) {

second.center = CGPointMake(165, 165);
[second setUserInteractionEnabled:NO];

theCentre = [[NSUserDefaults standardUserDefaults] init];

NSString *centre = NSStringFromCGPoint(second.center);

[theCentre setObject:centre forKey:@"secondCentre"];

// NSLog(@"%@", [theCentre objectForKey:@"centre"]);
[theCentre synchronize];
}
}
if (touch.view == third) {
third.center = location;

if (((third.center.x >= 200) && (third.center.y >= 200) &&
(third.center.x <= 230) && (third.center.y <= 230))) {

third.center = CGPointMake(215, 215);
[third setUserInteractionEnabled:NO];

theCentre = [[NSUserDefaults standardUserDefaults] init];

NSString *centre = NSStringFromCGPoint(third.center);

[theCentre setObject:centre forKey:@"thirdCentre"];

// NSLog(@"%@", [theCentre objectForKey:@"centre"]);
[theCentre synchronize];
}

}

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}

我尝试过快速枚举,但这使得一个 imageView 移动,而其他 imageView 就从屏幕上消失了!显然不是想要的效果...

最佳答案

我建议为此使用 UIGestureRecognizers。设置 ImageView 后写入:

NSArray *imageViews = [NSArray arrayWithObjects:first, second, third, nil];
for (UIView *imageView in imageViews) {
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] 
initWithTarget:self action:@selector(handlePanGesture:)]
[imageView addGestureRecognizer:panGR];
[panGR release];
}

然后实现手势处理程序:

- (void)handlePanGesture:(UIGestureRecognizer *)gestureRecognizer {
UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *)gestureRecognizer;
UIView *gestureView = [panGR view];
CGPoint translation = [panGR translationInView:gestureView];
gestureView.center = CGPointMake(gestureView.center.x + translation.x,
gestureView.center.y + translation.y);
[panGR setTranslation:CGPointZero inView:gestureView];
}

此代码未经测试(用记事本编写),但这是正确的方向。

关于cocoa - 在屏幕上拖动 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567535/

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