播放View 点击判断

//创建底部view
-(void)SV{
    // 设置底部view
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 60)];
    // 设置颜色
    view1.backgroundColor = [UIColor whiteColor];
    
    // 设置图片
    UIImageView *imgV1 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
    imgV1.image = [UIImage imageNamed:@"播放图片1"];
    imgV1.layer.masksToBounds = YES;
    imgV1.layer.cornerRadius = 25;
    [view1 addSubview:imgV1];
    
    [self.view addSubview:view1];
    // 设置文字
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(75, 8, 150, 20)];
    label.text = @"我的一个道姑朋友";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:16];
    [view1 addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(75, 30, 200, 20)];
    label1.text = @"左右横滑可切换上下首";
    label1.font = [UIFont systemFontOfSize:13];
//    label1.attributedText = [[NSAttributedString alloc]initWithString:@"左右横滑可切换上下首" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithPatternImage:[UIImage imageNamed:@"222"]]}];
    [view1 addSubview:label1];
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(250, 5, 40, 40)];
    [button setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(djbf) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button];
    
    //创建url 获取本地url
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"以冬-我的一个道姑朋友.mp3" withExtension:nil];
    //创建播放器
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
}

-(void)play:(UIButton *)sender{

if(sender.selected == NO){
    
    sender.selected = YES;
    //没播放是的图片
    [sender setImage:[UIImage imageNamed:@"bo"] forState:UIControlStateNormal];
    [self.player play];
    
}else if (sender.selected == YES){
    
    sender.selected = NO;
    //点击后的图片
    [sender setImage:[UIImage imageNamed:@"zanQ"] forState:UIControlStateNormal];
    [self.player pause];
}
}

猜你喜欢

转载自blog.csdn.net/qq_43361450/article/details/84140631