如何在视图控制器周围移动多个UIImage视图?

时间:2013-12-14 03:25:54

标签: uiimageview touch move uipangesturerecognizer

如何在视图控制器周围移动多个UIImage视图?

我已设法使用此代码;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch began --------- |n");


}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch moved --------- |n");


    UITouch *myTouch = [touches anyObject];
    startPoint = [myTouch locationInView:self.view];



    ball.center = CGPointMake(startPoint.x, startPoint.y);

    }


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch end --------- |n");

    }

上面的代码使得我可以通过触摸移动一个UIImage视图,但是,我希望能够移动100个左右。此代码目前,当您在屏幕区域移动手指时,图像会跳到您的手指上。

这是最好的方法吗?或者是更好的平移手势?

请帮助我这样做,这样我就可以通过触摸在我的视图控制器周围移动多个图像,如果您使用上面的代码,请停止图像跳跃!

请帮忙!

.H提交答案的文件;

@interface CMViewController : UIViewController {
    CGPoint startPoint;


}


@property CGPoint startPoint;

@property (strong, nonatomic) IBOutlet UIImageView *smyImageView;
@property (strong, nonatomic) IBOutlet UIImageView *smyImageView1;





@end

由于

2 个答案:

答案 0 :(得分:0)

这将教你如何知道自己想要做什么。

http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites

这是另一个可以帮助你的answer

答案 1 :(得分:0)

您可以在下方使用。我用它来移动屏幕上的多个图像。它对我有用。

UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan:)];

 [smyImageView addGestureRecognizer:span];

   UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan1:)];

   [smyImageView1 addGestureRecognizer:span1];

移动(平移):

- (void)onsPan:(UIPanGestureRecognizer *)recognizer {

   CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

- (void)onsPan1:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}