views:

400

answers:

1

I have a problem. Theres my View, with 2 UIButtons and an UIImageView. When a Button is pressed down then i want to move the Image, when it isn't touched down, I want to stop the UIImage. But I have no idea how this works. Maybe you can help me. Here's my Code:

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

I hope you can help me

thx

A: 

I believe you need to utilize the UIControlEventTouchDown, UIControlEventTouchUpInside and UIControlEventTouchUpOutside events.

On a touch-down, start moving the UIImage. On a touch-up, stop moving it.

EDIT: To continue moving the image, you might want to take a look at UIKit animations (accessible through UIView). There's also the UIControlEventTouchDownRepeat event, but I haven't used it personally, so I don't know if it's what you're looking for.

Can Berk Güder
This works, but when I hold on the UIButton the UIImage only moves 1 step to the left, but i wanted to move my Image permanently.
I updated my answer.
Can Berk Güder