按住UIButton +移动对象

时间:2009-07-10 18:51:24

标签: iphone cocoa-touch

我有问题。 Theres my View,有2个UIButtons和一个UIImageView。当按下按钮然后我想移动图像,当它没有被触及时,我想停止UIImage。但我不知道这是如何工作的。也许你可以帮助我。这是我的代码:

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

我希望你能帮助我

THX

1 个答案:

答案 0 :(得分:0)

我认为您需要利用UIControlEventTouchDownUIControlEventTouchUpInsideUIControlEventTouchUpOutside事件。

在降落时,开始移动UIImage。在修饰时,停止移动它。

编辑:要继续移动图片,您可能需要查看UIKit动画(可通过UIView访问)。还有UIControlEventTouchDownRepeat事件,但我没有亲自使用它,所以我不知道它是否是您正在寻找的。