2012-05-12 9 views
0

私は毎回コピーして貼り付けることができる私のアプリで次のコードを持っていますが、他の領域でも再利用できるようにしたいと思います。それは私が画面の周りにUIImageViewsをドラッグすることができますが、私はそれが非常に誤植で非常にしやすいという事実が好きではありません!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を動かし、他は画面から消えました!明らかに望ましい効果ではありません...

答えて

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

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

+0

これを行うにはいくつかのエラーがありますが、CGPoint変換ラインと最後のラインには表示されないインターフェイスがあると言います。何か案は? –

+1

おそらくUIGestureRecognizerには翻訳がないため、サブクラスUIPanGestureRecognizerだけです。更新されたコードを試してください。 – DrummerB

+0

おかげで、そのトリックをやった! –

関連する問題