0

私のアプリには次のコードがあり、毎回コピーして貼り付けることができますが、他の領域でも再利用できるように作成したいと思います。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];
}

高速列挙を試しましたが、1つのimageViewが移動し、他の画像が画面から消えてしまいました。明らかに望ましい効果ではありません...

4

1 に答える 1

2

これには UIGestureRecognizers を使用することをお勧めします。画像ビューを設定したら、次のように記述します。

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];
}

このコードはテストされていません (メモ帳に書かれています) が、これは正しい方向です。

于 2012-05-12T21:42:49.947 に答える