【iOS开发】 制作gif动态图

来源:互联网 发布:济南企业软件 编辑:程序博客网 时间:2024/04/28 06:43

//本部分代码在Xcode中可以运行 


代码

.h文件

//创建属性 图片和颜色

@property (nonatomicretainUIImageView *image;

@property (nonatomicretainUIColor *color;


.m文件

//释放内存

- (void)dealloc

{

    [_image release];

    [_color release];

    _image = nil;

    _color = nil;

    [super dealloc];

}

//在viewDidLoad中调用

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    [self.view setBackgroundColor:[UIColor cyanColor]];

    [self createView];


}

//创建所需要的类

- (void)createView{

    

    //创建滑动轮

    UISlider *lider = [[UISlider alloc]initWithFrame:CGRectMake(204028010)];

    [lider setBackgroundColor:[UIColor yellowColor]];//创建背景颜色

    lider.minimumTrackTintColor = [UIColor redColor];//拉动条左侧颜色设置

    [lider addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];//点击事件

    lider.minimumValue = 5.0;//控制最小播放速度

    lider.maximumValue = 10.0;//控制最大播放速度

    [self.view addSubview:lider];

    [lider release];

    

    //创建背景图和闪动图

    NSMutableArray *images = [NSMutableArray array];

    for (int i = 1; i < 9; i++) {//图片总个数+1 次循环

//图片最好改成有规律型的,利于在制作过程中,使代码能够简洁易懂

        NSString *name = [NSString stringWithFormat:@"%d.png",i];

        UIImage *aImige = [UIImage imageNamed:name];

        [images addObject:aImige];//把图片加到数组中,循环

    }

    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(2070280340)];//设置一个具体位置

    [_image setImage:[UIImage imageNamed:@"背景图.png"]];

    [_image setBackgroundColor:[UIColor greenColor]];

    _image.layer.borderColor = [[UIColor yellowColorCGColor];//设置边界颜色

    _image.layer.borderWidth = 5;//设置边界宽度 

    [_image startAnimating];//播放数组中图片循环所制作的动画

    [_image setAnimationImages:images];

    _image.userInteractionEnabled = YES;//图片可点击 方便于在此章代码上再添加一些功能

    [self.view addSubview:_image];

    [_image release];

    

    //创建按钮 (button按钮 不能release

    UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(204408030)];

    [startButton setBackgroundColor:[UIColor orangeColor]];//设置按钮背景色

    [startButton setTitleColor:[UIColor blackColorforState:UIControlStateHighlighted];//设置字体颜色

    [startButton setTitle:@"动画开始" forState:UIControlStateNormal];

    [startButton addTarget:self action:@selector(actionStart:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:startButton];

    

    UIButton *endButton = [[UIButton alloc]initWithFrame:CGRectMake(1204408030)];

    [endButton setBackgroundColor:[UIColor blueColor]];

    [endButton setTitleColor:[UIColor blackColorforState:UIControlStateHighlighted];

    [endButton setTitle:@"动画结束" forState:UIControlStateNormal];

    [endButton addTarget:self action:@selector(actionEnd:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:endButton];

    

    UIButton *restoreButton = [[UIButton alloc]initWithFrame:CGRectMake(2204408030)];

    [restoreButton setBackgroundColor:[UIColor orangeColor]];

    [restoreButton setTitleColor:[UIColor blackColorforState:UIControlStateHighlighted];

    [restoreButton setTitle:@"还原背景" forState:UIControlStateNormal];

    [restoreButton addTarget:self action:@selector(actionReturn:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:restoreButton];

    

}

//创建点击事件

#pragma mark lider点击事件

- (void)action:(id)sender{

 

    NSLog(@"%s", __func__);//习惯输出,方便测试程序

    

    UISlider *sil = (UISlider *)sender;//强转

    [_image setAnimationDuration:10 - sil.value];//获得播放速度值

    [_image startAnimating];//开始播放

    

}


#pragma mark 开始方法

- (void)actionStart:(id)sender{

    

    NSLog(@"%s", __func__);

    

    [_image startAnimating];

    

}


#pragma mark 结束方法

- (void)actionEnd:(id)sender{

    

    NSLog(@"%s", __func__);

    

    [_image stopAnimating];//结束播放

    

}


#pragma mark 还原背景色方法

- (void)actionReturn:(id)sender{

    

    NSLog(@"%s", __func__);

    

    [_image stopAnimating];

    [_image setImage:[UIImage imageNamed:@"背景图.png"]];

    

}



以上代码可以运行,如果有不足之处,希望告知,谢谢!!!


0 0
原创粉丝点击