2009-11-07 51 views
1

我有加速度计的Bounce类中的UIView类的子类。 此弹跳类显示图像并将其移动到屏幕上。当iPhone设备移动时,此图像在屏幕上弹跳。多个UIView实例不起作用

当我创建多个实例,只有最后一个实例工作properlty:

// in the MainViewController.m 

Bounce *heart[100]; 


for(int i = 0; i < 10; i++) { 
    rx = (arc4random() % 300) + 10; 
    ry = (arc4random() % 300) + 10; 
    NSLog(@"random %d %d", rx, ry); 
    heart[i] = [[Bounce alloc] initWithPNG:@"Heart.png" 
         position:CGPointMake(rx, ry) size:CGSizeMake(64, 64)]; 
    heart[i].velocity = CGPointMake(1.0, 1.0); 
    [self.view addSubview: heart[i]]; 
} 

这是Bounce类:

// 
// Bounce.h 
// iMakeLove 
// 
// Created by Giovambattista Fazioli on 06/11/09. 
// Copyright 2009 Saidmade srl. All rights reserved. 
// 

#import <UIKit/UIKit.h> 


@interface Bounce : UIView <UIAccelerometerDelegate> { 

    CGPoint  position; 
    CGSize  size; 
    CGPoint  velocity; 
    NSTimer  *objTimer; 
    NSString *pngName; 
    CGFloat  bounce; 
    CGFloat  gravity; 
    CGPoint  acceleratedGravity; 
    CGPoint  lastTouch; 
    CGPoint  currentTouch; 
    BOOL  dragging; 

    UIAccelerometer *accelerometer; 

} 



@property CGPoint position; 
@property CGSize size; 
@property CGPoint velocity; 
@property(nonatomic,retain)NSString *pngName; 
@property(nonatomic,retain)NSTimer *objTimer; 
@property CGFloat bounce; 
@property CGFloat gravity; 
@property CGPoint acceleratedGravity; 
@property CGPoint lastTouch; 
@property CGPoint currentTouch; 
@property BOOL dragging; 

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s; 

- (void)update; 
- (void)onTimer; 
- (void)startPrevent; 

@end 

实现:

// 
// Bounce.m 
// iMakeLove 
// 
// Created by Giovambattista Fazioli on 06/11/09. 
// Copyright 2009 Saidmade srl. All rights reserved. 
// 

#import "Bounce.h" 

@implementation Bounce 

@synthesize position, size; 
@synthesize objTimer; 
@synthesize velocity; 
@synthesize pngName; 
@synthesize bounce; 
@synthesize gravity, acceleratedGravity; 
@synthesize lastTouch, currentTouch; 
@synthesize dragging; 



- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s { 

    if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) { 

     [self setPngName:imageName]; 
     [self setPosition:p]; 
     [self setSize:s]; 
     [self setBackgroundColor:[UIColor clearColor]]; 

     // Set default gravity and bounce 

     [self setBounce:-0.9f]; 
     [self setGravity:0.5f]; 
     [self setAcceleratedGravity:CGPointMake(0.0, gravity)]; 
     [self setDragging:NO]; 

     UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)]; 

     prezzie.image = [UIImage imageNamed:imageName]; 

     [self addSubview:prezzie]; 

     [prezzie release]; 

     self.accelerometer = [UIAccelerometer sharedAccelerometer]; 
     self.accelerometer.delegate = self; 

    } 
    return self; 
} 

- (void)startPrevent { 
    if (objTimer == nil) { 
     objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 
    } 
} 


- (void)update { 

    [self setNeedsDisplay]; 

    if(dragging) return; 

    velocity.x += acceleratedGravity.x; 
    velocity.y += acceleratedGravity.y; 
    position.x += velocity.x; 
    position.y += velocity.y; 

    if(position.x + size.width >= 320.0) { 
     position.x = 320.0 - size.width; 
     velocity.x *= bounce; 
    } else if(position.x <= 0.0) { 
     velocity.x *= bounce; 
    } 

    if(position.y + size.height >= 480.0) { 
     position.y = 480.0 - size.height; 
     velocity.y *= bounce; 
    } else if(position.y <= 0.0) { 
     velocity.y *= bounce; 
    } 
    self.frame = CGRectMake(position.x, position.y, size.width, size.height); 
} 



- (void)onTimer { 
    [self update]; 
} 



- (void)drawRect:(CGRect)rect { 

    // Drawing code 

} 

/* EVENTS */ 


- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { 
    acceleratedGravity.x = acceleration.x * gravity; 
    acceleratedGravity.y = -acceleration.y * gravity; 
} 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    // First, lets check to make sure the timer has been initiated 

    if (objTimer == nil) { 
     objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 
    } 

    UITouch *touch = [touches anyObject]; 

    [self setCurrentTouch:[touch locationInView:self]]; 
    CGFloat dx = currentTouch.x - position.x; 
    CGFloat dy = currentTouch.y - position.y; 
    CGFloat dist = sqrt(dx * dx + dy * dy); 

    if(dist < size.width) { 
     [self setVelocity:CGPointMake(0.0, 0.0)]; 
     [self setDragging:YES]; 
    } 
    [self setLastTouch:currentTouch]; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [self setCurrentTouch:[touch locationInView:self]]; 
    [self setDragging:YES]; 
    [self setVelocity:CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y)]; 
    [self setLastTouch:currentTouch]; 

} 



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self setDragging:NO]; 
} 





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

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

你能帮助我吗?

+0

详情...会发生什么?出了什么问题?什么不行? – marcc

+0

如果您制作了10个实例,它们会在屏幕上显示,但 只有最后一个Bounce实例可以使用Accelerometer正常工作。 – Undolog

+0

Bounce.m? iMakeLove?我可以从这里看到应用拒绝:) –

回答

2

这是因为self.accelerometer.delegate = self;正在更改sharedAccelerometer的代表,只有最后一项分配才会生效。

0

我相信只有最前面的视图才会得到加速度信息。您可能希望将加速度计消息发送到您的viewcontroller。

+0

非常好的主意......谢谢... – Undolog

0

我可能会在这里做什么。 (它可能不是最好的东西,但会解决当前的问题)是这样的:

我想让加速引力成为所有Bounce实例之间共享的静态变量。这对我来说很重要,因为在现实世界中(我们模拟的那个)加速引力对于所有反弹都是一样的。 (我也会将Bounce重命名为BouncingView)。我会通过几个类方法访问该变量。此外,我会使加速度计共享,并有其委托方法,类方法。把这些东西放在你的课堂上,它应该可以工作:(我会测试它,但是我的开发机器在苹果商店中得到修复。)另外,你发布的示例代码中有几个语法错误。

static CGCGPoint acceleratedGravity; 
    static UIAccelerometer *accelerometer; 

    -(void) init { 
     /* Everything Else */ 
     self.accelerometer = [UIAccelerometer sharedAccelerometer]; 
     self.accelerometer.delegate = self; 
     /*Everything Else */ 
    } 

    +(CGPoint) acceleratedGravity { 
     return acceleratedGravity; 
    } 

    +(void) setAcceleratedGravity:(CGPoint) _acceleratedGravity { 
     acceleratedGravity = _acceleratedGravity; 
    } 

+ (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { 
    acceleratedGravity.x = acceleration.x * gravity; 
    acceleratedGravity.y = -acceleration.y * gravity; 
} 
+0

委托方法中的+会将它从实例范围移动到类范围。 –

+0

谢谢...语法错误在哪里? – Undolog

+0

没有主要的,rx和ry在你的主视图控制器for循环中没有拖延。 (至少不在代码片段中),而在Bounce类中,您在实现中使用self.accelerometer,但在头中没有属性声明(仅变量),并且没有@synthisize。你能够理清你的加速度计问题吗? –