0
我在我的應用程序中有以下代碼,每次我都可以複製和粘貼,但我寧願讓它在其他區域也可以重用。它允許我在屏幕上拖動UIImageView,但是我不喜歡這個錯誤很容易出錯的事實!在屏幕上拖動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移動,其他人只是消失在屏幕上!顯然不是想要的效果...
我這樣做了幾個錯誤,說CGPoint翻譯行中沒有可見的接口,也是最後一行。有任何想法嗎? –
也許是因爲UIGestureRecognizer沒有翻譯,只是它的子類UIPanGestureRecognizer。嘗試更新我的代碼。 – DrummerB
乾杯兄弟,這是訣竅! –