【问题标题】:iPhone Development -CGPoint & NSTimer helpiPhone 开发 -CGPoint & NSTimer 帮助
【发布时间】:2009-12-05 15:09:40
【问题描述】:

我想在视图中的特定区域跳球, 问题是我不知道如何定义这个领域区域和运动的位置。 还 这是我用来在整个视图中移动球的代码(球对象是视图上的 uiimageview)。 谢谢。

-(void) onTimer { ball.center = CGPointMake(ball.center.x+pos.x,ball.center.y+pos.y);

if(ball.center.x > 320 || ball.center.x < 0)
    pos.x = -pos.x;
if(ball.center.y > 460 || ball.center.y < 0)
    pos.y = -pos.y;

}

// 实现 viewDidLoad 以在加载视图后进行额外设置。 - (void)viewDidLoad { pos = CGPointMake(14.0,7.0);

[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

【问题讨论】:

  • 您需要澄清您的问题和/或将其分解为多个部分。您现在拥有的代码有什么问题,即您看到了什么意外行为?例如,球根本不动吗?他们会移动但移动不正确吗?定时器不调用onTimer:方法吗?
  • 在上面的代码中它工作正常(球在整个屏幕中移动)但问题是我似乎无法在视图中的特定位置定义一个小区域而不是 320\ 460。当我改变数值时,球会弹回原地或移动到错误的位置。

标签: iphone


【解决方案1】:

除了硬编码 320 和 460 之外,您还可以使用视图的边界来获取球应该移动的范围。

查看UIViewbounds 属性。

【讨论】:

    【解决方案2】:
    @implementation ViewController {
        BOOL isPositiveXAxis;
        BOOL isPositiveYAxis;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //By default we are setting both BOOL value to YES because ballImage have (0,0) origin by default 
        isPositiveXAxis = YES;
        isPositiveYAxis = YES;
        imgBallView.layer.cornerRadius = imgBallView.frame.size.width/2;
    
        [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateBall) userInfo:nil repeats:YES];
    }
    
    -(void)animateBall {
    
            //getting current image transform, and we will modify it later in code
            CGAffineTransform transform = imgBallView.transform;
    
    
            //checking if ‘isPositiveXAxis is true and imgBallView xAxis have less value then view border
            if (isPositiveXAxis && imgBallView.frame.origin.x < self.view.frame.size.width - imgBallView.frame.size.width) {
                transform.tx++;
            }
            else {
                isPositiveXAxis = NO;
    
                if (transform.tx>0) {
                    transform.tx--;
                }
                else {
                    isPositiveXAxis = YES;
                }
            }
    
            //checking if ‘isPositiveYAxis is true and imgBallView yAxis have less value then view border
            if (isPositiveYAxis && imgBallView.frame.origin.y < self.view.frame.size.height - imgBallView.frame.size.height) {
                transform.ty++;
            }
            else {
                isPositiveYAxis = NO;
    
                if (transform.ty>0) {
                    transform.ty--;
                }
                else {
                    isPositiveYAxis = YES;
                }
            }
    
            //setting updated trasform to imgBallView it will look animated.
            imgBallView.transform =  transform;
    }
    

    以上代码将生成所需的模式。可以在视频中看到:https://vid.me/G3zO,或许可以帮助你更好的理解

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多