【问题标题】:Trying to get a UIImageView to stop moving when it hits the bounds of the screen...help!试图让 UIImageView 在碰到屏幕边界时停止移动...帮助!
【发布时间】:2009-12-19 16:37:50
【问题描述】:

我正在制作一个简单的游戏应用程序,您可以在其中按下左/右按钮,然后 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];

}

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

【问题讨论】:

    标签: iphone animation uiimageview bounds


    【解决方案1】:

    在您的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] 来获取每个玩家的宽度。我不确定这种方法是否存在,但应该足以说明这个概念。

    【讨论】:

    • 现在 XCode 给了我错误:二进制/无效操作数(有 'id' 和 'int')两次
    • 这可能是因为您的播放器上没有-width() 方法。请参阅我帖子末尾的注释。我不确定播放器宽度使用什么。如果您提前知道它们的宽度,我想您可以对其进行硬编码。
    • 哦,对不起!图片的宽度是 91 px,所以我将 [player width]/2 替换为 45.5。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多