如何用iPhone的浪潮移动标签?

时间:2010-04-30 16:25:01

标签: iphone objective-c accelerometer

以下代码来自 Beginning iPhone 3 Development 第15章,使用加速度计控制球。 我想用标签代替球,只能在x方向移动。 但是,我不知道如何修改代码来实现这一目标。 谁能帮帮我吗? 太多了!!!!

- (id)initWithCoder:(NSCoder *)coder {

    if (self = [super initWithCoder:coder]) {
        self.image = [UIImage imageNamed:@"ball.png"];
        self.currentPoint = CGPointMake((self.bounds.size.width / 2.0f) +
                                        (image.size.width / 2.0f), 
                                        (self.bounds.size.height / 2.0f) + (image.size.height / 2.0f));

        ballXVelocity = 0.0f;
        ballYVelocity = 0.0f;
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
    [image drawAtPoint:currentPoint];
}


- (void)dealloc {
    [image release];
    [acceleration release];

    [super dealloc];
}

//#pragma mark -
- (CGPoint)currentPoint {

    return currentPoint;
}
- (void)setCurrentPoint:(CGPoint)newPoint {
    previousPoint = currentPoint;
    currentPoint = newPoint;

    if (currentPoint.x < 0) {
        currentPoint.x = 0;
        //ballXVelocity = 0;
        ballXVelocity = - (ballXVelocity / 2.0);
    }
    if (currentPoint.y < 0){
        currentPoint.y = 0;
        //ballYVelocity = 0;
        ballYVelocity = - (ballYVelocity / 2.0);
    } 
    if (currentPoint.x > self.bounds.size.width - image.size.width) {
        currentPoint.x = self.bounds.size.width  - image.size.width; 
         //ballXVelocity = 0;
        ballXVelocity = - (ballXVelocity / 2.0);
    }
    if (currentPoint.y > self.bounds.size.height - image.size.height) {
        currentPoint.y = self.bounds.size.height - image.size.height;
        //ballYVelocity = 0;
        ballYVelocity = - (ballYVelocity / 2.0);
    }

    CGRect currentImageRect = CGRectMake(currentPoint.x, currentPoint.y, 
                                         currentPoint.x + image.size.width, 
                                         currentPoint.y + image.size.height);
    CGRect previousImageRect = CGRectMake(previousPoint.x, previousPoint.y,
                                          previousPoint.x + image.size.width,  
                                          currentPoint.y + image.size.width);
    [self setNeedsDisplayInRect:CGRectUnion(currentImageRect,
                                            previousImageRect)];

}

- (void)draw {
    static NSDate *lastDrawTime;

    if (lastDrawTime != nil) {
        NSTimeInterval secondsSinceLastDraw =
        -([lastDrawTime timeIntervalSinceNow]);

        ballYVelocity = ballYVelocity + -(acceleration.y *
                                          secondsSinceLastDraw);
        ballXVelocity = ballXVelocity + acceleration.x *
        secondsSinceLastDraw;

        CGFloat xAcceleration = secondsSinceLastDraw * ballXVelocity * 500;
        CGFloat yAcceleration = secondsSinceLastDraw * ballYVelocity * 500;

        self.currentPoint = CGPointMake(self.currentPoint.x + xAcceleration,
                                        self.currentPoint.y +yAcceleration);
    }
    // Update last time with current time
    [lastDrawTime release];
    lastDrawTime = [[NSDate alloc] init];

}

1 个答案:

答案 0 :(得分:1)

首先,将标签创建为控制器的属性。如果您使用IB,那么将其作为IBOutlet并将其连接起来。然后在上面的代码中,它使用球图像,替换标签。当然,您不必绘制它将自己处理的标签。

除了移动标签的中心或原点而不是在矩形中绘制球外,移动逻辑完全相同。

修改

  谢谢你!但我不太明白   如何移动标签的中心?

像这样:

self.myLabel.center=CGPointMake(newX,newY);

您还可以为更改设置动画,以使标签看起来平滑移动。有关方法,请参阅UIView类docs。