自定义播放暂停按钮Iphone

时间:2010-02-16 11:40:20

标签: iphone graphics custom-controls

我想问一下使用cocoa for IPhone实现以下编码的最佳方法是什么。

作为一个学习练习,我想制作一个非常简单的MP3播放器。我目前最感兴趣的部分是该播放器的自定义视觉组件。

特别是如何使用鼠标悬停和mousedown状态制作自定义播放暂停按钮?

此外,我希望有一个“抽屉”打开和关闭,具体取决于播放/暂停按钮显示其播放或暂停状态。

当点击播放时,按钮图标变为暂停图标,抽屉滑动打开。当暂停时,暂停图标变为播放图标,抽屉将关闭。

我应该看石英吗?

2 个答案:

答案 0 :(得分:2)

无需使用Quartz。

UIButton开始。您可以只为一个按钮指定一个图像(如果您想要自定义突出显示时的外观(setImage:forState:),它可以处理在触摸时突出显示它)或为每个按钮状态指定不同的图像。要实现播放和暂停之间的切换效果,您应该在点击按钮时替换按钮的图像(在UIControlEventTouchUpInside的操作中)。

对于抽屉动画,请创建一个UIView作为抽屉并将其添加到主视图中。调整其框架,使其放在屏幕外。然后,当您想要将其设置为屏幕动画时,请使用简单的动画块:

[UIView beginAnimations:nil context:nil];
drawerView.center = CGPointMake(...); // set coordinate to the end position
[UIView commitAnimations];

尝试阅读文档中的所有内容。如果您还有问题,请询问更具体的问题。

答案 1 :(得分:1)

我接受了您的建议并访问了开发参考中心。更改播放暂停暂停按钮的状态非常简单。

然而,将我的面板子视图添加到我的UIVIEWController和动画对我来说非常困难(objective -c新手)。

首先,当我尝试制作我的小组视图时,我收到警告说小组UIView may not respond to initWithFrame. (initwithframe is a function of UIView ?)

其次,我无法将图像添加到我的面板UIView中。我收到警告说

warning: passing argument 1 of 'addSubview:' from distinct Objective-C type

最后,当我尝试将动画应用到我的面板时,我会收到错误

UIView'可能无法响应'-commitAnimations'以及我尝试添加的任何其他动画相关方法。

你能帮忙吗?我认为主要是我不明白为什么我得到这些“可能没有响应方法”警告,因为我正在调用正确类型的方法(或者我认为)

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    // SET UP THE PLAY PAUSE BUTTON
    playPause = [UIButton buttonWithType:UIButtonTypeCustom];
    playPause.frame = CGRectMake(-20,280, 100,100);
    [playPause setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    [playPause addTarget:self action:@selector(buttonPushed:) 
        forControlEvents:UIControlEventTouchUpInside];
    playing=NO;

    //SET UP THE AUDIO PLAYER
    NSLog(@"Set up theAudio");
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/DumpTheChump.mp3", [[NSBundle mainBundle] resourcePath]]];  
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;

    //SETUP THE BOTTOM part of the PANEL
    UIImage *panelBottomImage=[UIImage imageNamed:@"panel_bottom.png"];
    panelBottom=[UIView initWithFrame:(CGRectMake(-20, 280, 285, 128))];
    [panelBottom addSubview:panelBottomImage];

    //SETUP THE TOP PART OF THE PANEL. (THE ONE THAT SHOULD SLIDE UP AND DOWN)

    UIImage *panelTopImage=[UIImage imageNamed:@"panel_top.png"];
    panelTop=[UIView initWithFrame:(CGRectMake(-20, 280, 285, 128))];
    [panelTop addSubview:panelTopImage];


    [self.view addSubview:panelBottom];
    [self.view addSubview:panelTop];
    [self.view addSubview:playPause];

//  285*128

}

- (void) buttonPushed:(id)sender
{
    NSLog(@"It works!");

    switch (playing) {
        case NO:
            playing=YES;
            [audioPlayer play];
            [playPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            [panelTop beginAnimations:nil context:nil];
            [panelTop setAnimationDuration:2];
            [panelTop setAnimationDelegate:self];
            //UIView setAnimationDidStopSelector:@selector(onAnimationC omplete:finished:context;

            // set the position where button will move to
            panelTop.frame = CGRectMake(-20, 100, 285, 128);    

            [panelTop commitAnimations];
            break;
        case YES:
            playing=NO;
            [audioPlayer pause];
              [playPause setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
            [playPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            [panelTop beginAnimations:nil context:nil];
            [panelTop setAnimationDuration:2];
            [panelTop setAnimationDelegate:self];
            //UIView setAnimationDidStopSelector:@selector(onAnimationC omplete:finished:context;

            // set the position where button will move to
            panelTop.frame = CGRectMake(-20, 280, 285, 128);    

            [panelTop commitAnimations];

            break;
        default:
            break;
    }
}
相关问题