如何随着iPhone的挥动来移动标签?

发布于 2024-08-30 13:07:54 字数 3293 浏览 2 评论 0原文

下面的代码来自《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];

}

The code below is from the book Beginning iPhone 3 Development, chapter 15, using the accelerometer to control a ball.
I want to use a label instead of the ball, and only move in the x direction.
However, I donno how to revise the code to achieve this goal.
Can anyone please help me?
THX so much!!!!

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

}

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

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

发布评论

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

评论(1

很快妥协 2024-09-06 13:07:54

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

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

编辑

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

像这样:

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

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

To start, you create the label as a property of the controller. If you use IB then make it an IBOutlet and hook it up. Then in the code above, where it uses the ball image, substitute the label. Of course, you don't have to draw the label it will handle that itself.

The movement logic is exactly the same except you move the center or origin of the label instead of drawing the ball in a rect.

Edit

thank u! But I am not quite understand
how to move the center of the label?

Like this:

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

You can also animate the change so that the label appears to move smoothly. See the UIView class docs for the methods.

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