试图让 UIImageView 在到达屏幕边界时停止移动...救命!

发布于 2024-08-15 15:15:50 字数 2412 浏览 4 评论 0原文

我正在制作一个简单的游戏应用程序,您按下左/右按钮,UIImageView 就会移动并产生动画。问题是 - 我希望 UIImageView 在接触时停止移动


self.view.bounds.size.width

我该怎么做?

这是现有的代码:


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == rightbutton){
 NSLog(@"touch rightbutton");
 player.animationImages = [NSArray arrayWithObjects:
         [UIImage imageNamed:@"linkr1.png"],
         [UIImage imageNamed:@"linkr2.png"],
         [UIImage imageNamed:@"linkr3.png"],
         [UIImage imageNamed:@"linkr4.png"],
         [UIImage imageNamed:@"linkr5.png"],
         [UIImage imageNamed:@"linkr6.png"],
         [UIImage imageNamed:@"linkr7.png"],
         [UIImage imageNamed:@"linkr8.png"],
         nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating];  
 rightbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rightrepeater) userInfo:nil repeats:YES];
}
if([touch view] == leftbutton){
 NSLog(@"touch leftbutton");
 player.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"linkr1l.png"],
        [UIImage imageNamed:@"linkr2l.png"],
        [UIImage imageNamed:@"linkr3l.png"],
        [UIImage imageNamed:@"linkr4l.png"],
        [UIImage imageNamed:@"linkr5l.png"],
        [UIImage imageNamed:@"linkr6l.png"],
        [UIImage imageNamed:@"linkr7l.png"],
        [UIImage imageNamed:@"linkr8l.png"],
        nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating]; 
 leftbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(leftrepeater) userInfo:nil repeats:YES];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [[event allTouches] anyObject];
 if([touch view] == rightbutton){
  NSLog(@"let go of rightbutton");
  [rightbuttontimer invalidate];
  [player stopAnimating];
 }
  if([touch view] == leftbutton){
  NSLog(@"let go of leftbutton");
  [leftbuttontimer invalidate];
   [player stopAnimating];
 }
}

- (void)rightrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += 10; 
 [player setCenter:center];

}

- (void)leftrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += -10; 
 [player setCenter:center];

}

任何帮助将不胜感激。谢谢!

I am making a simple game app where you press a left/right button, and a UIImageView moves and animates. Here is the problem - I want the UIImageView to stop moving when it touches


self.view.bounds.size.width

How do I do this?

Here is the existing code :


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == rightbutton){
 NSLog(@"touch rightbutton");
 player.animationImages = [NSArray arrayWithObjects:
         [UIImage imageNamed:@"linkr1.png"],
         [UIImage imageNamed:@"linkr2.png"],
         [UIImage imageNamed:@"linkr3.png"],
         [UIImage imageNamed:@"linkr4.png"],
         [UIImage imageNamed:@"linkr5.png"],
         [UIImage imageNamed:@"linkr6.png"],
         [UIImage imageNamed:@"linkr7.png"],
         [UIImage imageNamed:@"linkr8.png"],
         nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating];  
 rightbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rightrepeater) userInfo:nil repeats:YES];
}
if([touch view] == leftbutton){
 NSLog(@"touch leftbutton");
 player.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"linkr1l.png"],
        [UIImage imageNamed:@"linkr2l.png"],
        [UIImage imageNamed:@"linkr3l.png"],
        [UIImage imageNamed:@"linkr4l.png"],
        [UIImage imageNamed:@"linkr5l.png"],
        [UIImage imageNamed:@"linkr6l.png"],
        [UIImage imageNamed:@"linkr7l.png"],
        [UIImage imageNamed:@"linkr8l.png"],
        nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating]; 
 leftbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(leftrepeater) userInfo:nil repeats:YES];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [[event allTouches] anyObject];
 if([touch view] == rightbutton){
  NSLog(@"let go of rightbutton");
  [rightbuttontimer invalidate];
  [player stopAnimating];
 }
  if([touch view] == leftbutton){
  NSLog(@"let go of leftbutton");
  [leftbuttontimer invalidate];
   [player stopAnimating];
 }
}

- (void)rightrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += 10; 
 [player setCenter:center];

}

- (void)leftrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += -10; 
 [player setCenter:center];

}

Any help will be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-08-22 15:15:50

在您的 rightRepeater 方法中:

- (void)rightrepeater {
    CGPoint center = (CGPoint)[player center];
    center.x += 10;
    CGFloat rightEdge = center.x + [player width]/2;
    if (rightEdge > view.bounds.size.width) {
        center.x -= (rightEdge - view.bounds.size.width);
    }
    [player setCenter:center];
}

并且,在您的 leftRepeater 方法中类似:

- (void)leftrepeater {
    CGPoint center = (CGPoint)[player center]; 
    center.x -= 10;
    CGFloat leftEdge = center.x - [player width]/2;
    if (leftEdge < 0) {
        center.x -= leftEdge;
    }
    [player setCenter:center];
}

请注意,我使用 [player width] 来获取每个播放器的宽度。我不确定这样的方法是否存在,但它应该足以说明这个概念。

In your rightRepeater method:

- (void)rightrepeater {
    CGPoint center = (CGPoint)[player center];
    center.x += 10;
    CGFloat rightEdge = center.x + [player width]/2;
    if (rightEdge > view.bounds.size.width) {
        center.x -= (rightEdge - view.bounds.size.width);
    }
    [player setCenter:center];
}

And, similarly in your leftRepeater method:

- (void)leftrepeater {
    CGPoint center = (CGPoint)[player center]; 
    center.x -= 10;
    CGFloat leftEdge = center.x - [player width]/2;
    if (leftEdge < 0) {
        center.x -= leftEdge;
    }
    [player setCenter:center];
}

Note that I used [player width] to obtain the width of each player. I'm not sure if such a method exists, but it should be enough to illustrate the concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文