GIF动图 SegmentControl

准备工作不赘述

放入三组连续的图片
MainViewController.m

#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@property(nonatomic,retain)UIImageView *animation1;
@property(nonatomic,retain)UIImageView *animation2;
@property(nonatomic,retain)UIImageView *animation3;
@property(nonatomic,retain)NSMutableArray *picArr1;
@property(nonatomic,retain)NSMutableArray *picArr2;
@property(nonatomic,retain)NSMutableArray *picArr3;
@end

@implementation MainViewController
-(void)dealloc
{
    [_animation1 release];
    [_animation2 release];
    [_animation3 release];
    [_picArr1 release];
    [_picArr2 release];
    [_picArr3 release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.view.

    self.animation1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
    [self.view addSubview:self.animation1];
    [self.animation1 release];
    self.animation1.animationDuration=7;
    self.animation1.animationRepeatCount=7;
    self.picArr1=[NSMutableArray array];
    for (NSInteger i=0; i<81; i++) {
        NSString *picName=[NSString stringWithFormat:@"drink_%02ld.jpg",i];
        UIImage *image=[UIImage imageNamed:picName];
        [self.picArr1 addObject:image];
    }
    self.animation1.animationImages=self.picArr1;
//    [self.animation1 startAnimating];




    self.animation2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.animation2];
    [self.animation2 release];
    self.animation2.animationDuration=2;
    self.animation2.animationRepeatCount=7;
    self.picArr2=[NSMutableArray array];
    for (NSInteger i=0; i<28; i++) {
        NSString *picName=[NSString stringWithFormat:@"fart_%02ld.jpg",i];
        UIImage *image=[UIImage imageNamed:picName];
        [self.picArr2 addObject:image];
    }
    self.animation2.animationImages=self.picArr2;
//    [self.animation2 startAnimating];


    self.animation3=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT )];
    [self.view addSubview:self.animation3];
    [self.animation3 release];
    self.animation3.animationDuration=7;
    self.animation3.animationRepeatCount=7;
    self.picArr3=[NSMutableArray array];
    for (NSInteger i=0; i<81; i++) {
        NSString *picName=[NSString stringWithFormat:@"knockout_%02ld.jpg",i];
        UIImage *image=[UIImage imageNamed:picName];
        [self.picArr3 addObject:image];
    }
    self.animation3.animationImages=self.picArr3;
//    [self.animation3 startAnimating];


    UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:@[@"drink",@"fart",@"knockout"]];
    seg.frame=CGRectMake(50, 600, 250, 50);
    seg.layer.borderWidth=1;
    seg.layer.cornerRadius=10;
    seg.layer.masksToBounds=YES;
    [self.view addSubview:seg];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [seg release];

}
-(void)segAction:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex==0) {
        [self.animation1 startAnimating];
    }
    else if (seg.selectedSegmentIndex==1)
    {
        [self.animation2 startAnimating];
    }
    else if (seg.selectedSegmentIndex==2)
    {
        [self.animation3 startAnimating];
    }
}

你可能感兴趣的:(UI)