UIControl 音乐播放 gif动图

AppDelegate里的准备工作不再赘述

MainViewController.m

#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface MainViewController ()
@property(nonatomic,retain)UIStepper *stepper;
@property(nonatomic,retain)AVAudioPlayer *player;
@property(nonatomic,retain)UIImageView *animation;
@property(nonatomic,retain)NSMutableArray *picArr;
@property(nonatomic,retain)UISlider *slider;

@end

@implementation MainViewController
-(void)dealloc
{
    [_slider release];
    [_animation release];
    [_picArr release];
    [_player release];
    [_stepper release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.



    self.stepper=[[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:self.stepper];
    [_stepper release];

    //每次增加的值
    self.stepper.stepValue=0.5;

    // 添加一个点击方法
    [self.stepper addTarget:self action:@selector(stepAction:) forControlEvents:UIControlEventValueChanged];


    // 做本地的audioplayer
    // 找本地路径
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Teen Top - To You" ofType:@"mp3"];
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [self.player play];

    // 播放进度显示框
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 400, 300, 30)];
    label.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:label];
    [label release];


    [self.player release];

    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.png"]];
    imageView.frame=CGRectMake(0, 0, self.view.frame.size.width, 200);
    [self.view addSubview:imageView];
    [imageView release];

    // 先创建一个毛玻璃效果
    UIBlurEffect *effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    // 创建一个毛玻璃视图,指定效果   iOS8.0之后出现的效果
    UIVisualEffectView *effectView=[[UIVisualEffectView alloc]initWithEffect:effect];
    effectView.frame=imageView.frame;
    [imageView addSubview:effectView];
// UISlider
    self.slider=[[UISlider alloc]initWithFrame:CGRectMake (10,300, 300, 10)];

    [self.view addSubview:self.slider];
    [_slider release];

    // 最大值,最小值
    self.slider.minimumValue=0.0;
    self.slider.maximumValue=1;
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

-(void)sliderAction:(UISlider *)slider
{
    NSLog(@"%g",slider.value);
    // 播放的时长,被slider控制
//    self.animation.animationDuration=self.slider.value;

    // 控制播放音量
    self.player.volume=self.slider.value;

    // 设置播放进度
    [self.player pause];

    // 计算新的播放时间

    self.player.currentTime=self.slider.value * self.player.duration;
    // 重新start
    [self.animation startAnimating];
}

-(void)stepAction:(UIStepper *)stepper
{

    NSLog(@"%g",stepper.value);

    self.player.volume=self.stepper.value;


}

gif动图

// 播放gif图
    self.animation=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    [self.view addSubview:self.animation];
    [_animation release];

    // 容器使用之前,一定要对容器进行初始化
    self.picArr=[NSMutableArray array];

    for (NSInteger i=0;i<26 ; i++) {
        // 拼接图片的名称
        NSLog(@"%02ld",i);
        NSString *picName=[NSString stringWithFormat:@"angry_%02ld.jpg",i];
        NSLog(@"%@",picName);
        // 根据图片名找到对应的图片
        UIImage *image=[UIImage imageNamed:picName];

        // 把图片添加到数组中
        [self.picArr addObject:image];


    }

    self.animation.animationImages=self.picArr;

    self.animation.animationDuration=2;
    self.animation.animationRepeatCount=77;
    [self.animation startAnimating];


UISegmentedControl *SC=[[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
    SC.backgroundColor=[UIColor redColor];
    [self.view addSubview:SC];
    SC.frame=CGRectMake(10, 350, 350, 30);
    [SC addTarget:self action:@selector(sA:) forControlEvents:UIControlEventValueChanged];
    [SC release];
-(void)sA:(UISegmentedControl *)sc
{
    // 按钮默认从0开始
    NSLog(@"%ld",sc.selectedSegmentIndex);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值