【问题标题】:How can I move multiple UIImage views around a view controller?如何在视图控制器周围移动多个 UIImage 视图?
【发布时间】:2013-12-14 03:25:53
【问题描述】:

如何在视图控制器周围移动多个 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

谢谢

【问题讨论】:

    标签: uiimageview touch move uipangesturerecognizer


    【解决方案1】:

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

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

    【讨论】:

    • 我认为我会为每个图像视图添加“span2,span3”等是否正确?
    • 我会的!我将在大约 7 小时内实现它。
    • 我要把这个放在 viewDidLoad 中吗?
    • 你好,我已经在 viewDidLoad 中实现了这个,Xcode 已经接受了代码,我运行了应用程序,我不能移动 UIViews,如果这对你有用,我必须能够解决它在职的。也许我的 .h 不正确?我已经用我的 .h 更新了我的问题中的代码
    • 你为什么使用这一行 - (IBAction)onsPan:(id)sender;?这没有任何意义。
    【解决方案2】:

    这将教你所有你想做的事情。

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

    这是另一个answer,可能会对您有所帮助

    【讨论】:

    • 非常感谢您的回答!我没有使用过之前设置的Spritekit游戏项目。你能在 Spritekit 游戏中使用故事板吗?还是我必须完全以编程方式工作?今晚我会在我的电脑前浏览你发送的这个很棒的链接。
    • 部分部分可以使用storyboard,我只用来制作设定画面等。不过一般sprite kit都是代码。本教程将帮助您入门。如果你只想要 iOS,你可以使用相同的原理,只需使用标签设置要移动的图像视图。让我知道你的情况。
    • 我会打!我会在尝试后立即投票给答案,别担心!感谢您的回复。
    • 这是一个很棒的教程,但是,当构建视图控制器时,如何选择图像在视图控制器中的放置位置?我正在轻松添加 100 张都需要可移动的图像,无论如何我可以参考故事板上的图像吗?这将使事情变得容易得多。
    • 有没有办法让上面的代码引用不止一张图片?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多