2009-07-10 8 views
0

Ich habe ein Problem. Theres meine Ansicht, mit 2 UIButtons und einem UIImageView. Wenn eine Schaltfläche gedrückt ist, möchte ich das Bild verschieben, wenn es nicht gedrückt wird, möchte ich die UIImage stoppen. Aber ich habe keine Ahnung, wie das funktioniert. Vielleicht kannst du mir helfen. Hier ist mein Code:Halten Sie eine UIButton + Bewegen Sie ein Objekt

- (void)viewDidLoad 
{ 
    currentPositionx = 150.0; 
    currentPositiony = 150.0; 
    currentSizex = 147.0; 
    currentSizey = 146.0; 
    currentSpeed = 0.70; 


    contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    [contentView setImage:[UIImage imageNamed:@"BlackScreen.png"]]; 
    [contentView setUserInteractionEnabled:YES]; 
    [contentView setMultipleTouchEnabled:YES]; 
    self.view = contentView; 
    [contentView release]; 


    // create the view that will execute our animation 
    currentPlayer = [[UIImageView alloc] initWithFrame:CGRectMake(currentPositionx, currentPositiony, currentSizex, currentSizey)]; 

    // load all the frames of our animation 
    currentPlayer.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageNamed:@"RunnerRED_0.png"], 
            Nil]; 


    currentPlayer.animationDuration = currentSpeed; 
    currentPlayer.animationRepeatCount = 0; 
    [currentPlayer startAnimating]; 
    [self.view addSubview:currentPlayer]; 
    [currentPlayer release];  

    UIButton *GoLeftButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(20, 263, 64, 64)]; 
    [GoLeftButton addTarget:self action:@selector(GoLeft:) forControlEvents:UIControlEventTouchUpInside]; 
    [GoLeftButton setBackgroundImage:[UIImage imageNamed:@"Arrow Left RED.png"] forState:UIControlStateNormal]; 
    [self.view addSubview:GoLeftButton]; 

    UIButton *GoRightButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(100, 263, 64, 64)]; 
    [GoRightButton addTarget:self action:@selector(GoRight:) forControlEvents:UIControlEventTouchUpInside]; 
    [GoRightButton setBackgroundImage:[UIImage imageNamed:@"Arrow Right RED.png"] forState:UIControlStateNormal]; 
    [self.view addSubview:GoRightButton]; 

} 

-(void)GoLeft:(id)sender 
{ 

    CGRect frame = currentPlayer.frame; 
    frame.origin.x = frame.origin.x - 10; 
    currentPlayer.frame = frame; 
    currentPlayer.transform = CGAffineTransformIdentity; 
    currentPlayer.transform = CGAffineTransformMakeScale(-1.0, 1.0); 
} 


-(void)GoRight:(id)sender 
{ 
    CGRect frame = currentPlayer.frame; 
    frame.origin.x = frame.origin.x + 10; 
    currentPlayer.frame = frame; 
    currentPlayer.transform = CGAffineTransformIdentity; 
    currentPlayer.transform = CGAffineTransformMakeScale(1.0, 1.0); 
} 

@end 

Ich hoffe, dass Sie mir helfen können,

thx

Antwort

0

Ich glaube, Sie brauchen, um die UIControlEventTouchDown, UIControlEventTouchUpInside und UIControlEventTouchUpOutside Veranstaltungen zu nutzen.

auf einem Touch-down, starten Sie den UIImage bewegen. Bei einer Ausbesserung, hör auf, es zu bewegen.

EDIT: Um weiterhin das Bild bewegen, könnten Sie einen Blick auf UIKit Animationen (erreichbar über UIView) nehmen wollen. Es gibt auch das UIControlEventTouchDownRepeat Ereignis, aber ich habe es nicht persönlich benutzt, also weiß ich nicht, ob es das ist, wonach du suchst.

+0

Das funktioniert, aber wenn ich den UIButton halte, bewegt sich der UIImage nur einen Schritt nach links, aber ich wollte mein Bild dauerhaft bewegen. –

+0

Ich habe meine Antwort aktualisiert. –

Verwandte Themen