iOS开发UIImage和UIImageView属性介绍,实现图片动画,实现开始/停止按钮效果

来源:互联网 发布:大学机械制图软件 编辑:程序博客网 时间:2024/04/30 20:39

转载自:http://blog.csdn.net/weisubao/article/details/39553269

(1)如果是按钮触发一个事件方法,我们只需要用(id)sender把控件对象传递进来,这个方法就能处理控件属性值;而如果方法需要处理其他对象,那么一个方法,就是把这个对象设置为全局变量,这样所有的方法都可以使用这个对象,并设置它的属性,我们这里的imgView2就是这样,可以在方法中,控制它停止还是开始。


(2)UIImage和UIImageView通常是一对,UIImage的对象只是把图片添加到程序里面,但它不是视图无法被加载显示在APP中,但是UIImageView是一个视图,可以把UIImage的对象初始化给UIImageView的对象,然后让这个UIImageView的对象被加载到视图中以显示出来。从我们最后用了[self.view addSubview:imgView1]可见,我们要加载的虽然是一个子视图,但子视图是视图。


(3)图片的内容模式是比较重要的,即把图片怎么放在UIImageView的frame框里,是剧中、靠左还是等比例缩放填满,还是不等比例只要填满即可。


(4)实现动画效果,其实就是弄一系列图片然后给UIImageView对象的动画图片属性,如本例中所示一般是吧图片装在数组中,然后再传递;传递后设置播放时间和次数等等,最后用startAnimating和stopAnimating等方法来控制是否播放,当然,要记得把这视图加载进来才能显示。


(5)复习了一遍UIButton的方法,即如何实现像播放器的“点暂停就变成播放,点播放就变成暂停”的按钮的样式。

*发现在iOS7这些新版本中tintColor属性(设置点击时的按钮背景)失效了,所以为了点击时有点feeling,就用改变背景颜色的方法代替。

#import "ViewController.h"    @interface ViewController ()    @end    @implementation ViewController  {      //为了在其他方法中调用这个对象,需要把它设置为全局变量,而不仅仅是在viewDidLoad中      UIImageView *imgView2;  }    - (void)viewDidLoad {      //UIImage实例化一个img1对象,并初始化一个图片给它,这步骤相当于把图片移动到应用程序里面      UIImage *img1=[UIImage imageNamed:@"logo.png"];      //然后UIImageView相当于一个载体,用来显示这个图片对象      //图片自己不能在程序中显示,需要一个载体,如之前讲UIButton时,图片就是借助按钮这个载体显示的      //所以UIImage的对象img1没有frame框架      //只有UIImageView的对象imgView1才有frame属性,它装着图片,然后我们对它位置大小进行设置即可      UIImageView *imgView1=[[UIImageView alloc]initWithImage:img1];      //意料之中:图片被自动缩放了,缩放到frame的大小      imgView1.frame=CGRectMake(30, 30, 300, 80);      //那如果需要原图小小呢?      //(1)如果知道原图宽高,直接把数字写进去      //(2)调用图片size里地宽高属性,如imgView1.frame=CGRectMake(30, 30, img1.size.width, img1.size.height)            //随之而来的时,如果我既想保持imgView1的宽高不变,又想图片不被缩放      //需要用到内容模式属性      //先弄个背景来做效果演示用      imgView1.backgroundColor=[UIColor redColor];      //尝试其中一个,发现居中了,而且图片没有被缩放了      //这个contentMode内容模式是UIView的方法,不仅仅是UIImage(继承自UIView)的方法,所以我们看它的类型前面有UIView,如UIViewContentModeCenter  //    [imgView1 setContentMode:UIViewContentModeCenter];      //其他类型都是固定位置的,如上下左右左下右上等,下面几个是重点和常用的      //UIViewContentModeScaleToFill是填满,当然图片会被不等比例的缩放,和没有内容模式属性前一样      //UIViewContentModeScaleAspectFill是等比例缩放的填满,可以想象有一边长度正好,还有一边有可能超出了整个imgView1的frame边界      //UIViewContentModeScaleAspectFit是等比例缩放的填充到最大尺寸,可以想见有一边长度正好,还有一边有可能还没达到frame的边界      [imgView1 setContentMode:UIViewContentModeCenter];      [self.view addSubview:imgView1];            //设置动画      //先弄一个UIImageView准备加载,本来是UIImageView *imgView2=[[UIImageView alloc]init]但imgView2已在顶部声明为全局变量,此处只需初始化即可      imgView2=[[UIImageView alloc]init];      imgView2.frame=CGRectMake(30, 150, 40, 40);      //把需要动画的一系列图片导入到一个数组中,少的话就不需要用for循环导入      NSMutableArray *arr1=[[NSMutableArray alloc]init];      for (int i=1; i<=3; i++) {          UIImage *img2=[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",i]];          [arr1 addObject:img2];      }      //1、先把这个数组装到动画图片中      imgView2.animationImages=arr1;      //2、设置播放时间,即每次全部完全播放完的时间      imgView2.animationDuration=1;      //3、设置重复播放次数,如不设置或为0时,表示无限循环      imgView2.animationRepeatCount=0;      //4、还要启动,才能播放      [imgView2 startAnimating];      //当然还要加载进来才能显示      [self.view addSubview:imgView2];            //我们再增加一个按钮,来实现开始和停止功能      UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];      btn1.frame=CGRectMake(30, 250, 200, 30);      [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];      btn1.backgroundColor=[UIColor grayColor];      btn1.showsTouchWhenHighlighted=YES;      [btn1 setTitle:@"停止" forState:UIControlStateNormal];      btn1.tag=1;      [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            [self.view addSubview:btn1];      [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.  }    //按钮实现的是:点击停止,则按钮变成“开始”,背景绿色,然后动画停止。点击开始,按钮变成“停止”,背景灰色。  //这种按钮的功能其实就类似于播放/暂停键  -(void)btnClick:(id)sender{      UIButton *btn=(UIButton *)sender;      if ((int)btn.tag==1) {          [btn setTitle:@"开始" forState:UIControlStateNormal];          btn.backgroundColor=[UIColor greenColor];          [imgView2 stopAnimating];          btn.tag=2;      }else{          [btn setTitle:@"停止" forState:UIControlStateNormal];          btn.backgroundColor=[UIColor grayColor];          [imgView2 startAnimating];          btn.tag=1;                }  }    - (void)didReceiveMemoryWarning {      [super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated.  }    @end  

我的代码

-(void) viewDidLoad{    UIImage* img1 = [UIImage imageNamed:@"propIcon_50000001.png"];    UIImageView* imgView1 = [[UIImageView alloc]initWithImage:img1];    imgView1.frame = CGRectMake(30, 30, 300, 80);    imgView1.backgroundColor = [UIColor redColor];    [imgView1 setContentMode:UIViewContentModeCenter];    [self.view addSubview:imgView1];        imgView2 = [[UIImageView alloc]init];    imgView2.frame = CGRectMake(30, 150, 40, 40);    NSMutableArray* arr1 = [[NSMutableArray alloc]init];    for(int i = 1; i <= 9; i++)    {        UIImage* img2 = [UIImage imageNamed:[NSString stringWithFormat:@"propIcon_5000000%i.png",i]];        [arr1 addObject:img2];    }    imgView2.animationImages = arr1;    imgView2.animationDuration = 1;    imgView2.animationRepeatCount = 0;    [imgView2 startAnimating];    [self.view addSubview:imgView2];        UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeCustom];    btn1.frame = CGRectMake(30, 250, 200, 30);    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    btn1.backgroundColor = [UIColor grayColor];    btn1.showsTouchWhenHighlighted = YES;    [btn1 setTitle:@"停止" forState:UIControlStateNormal];    btn1.tag = 1;    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn1];        [super viewDidLoad];}-(void) btnClick:(id)sender{    UIButton* btn = (UIButton*) sender;    if((int)btn.tag == 1)    {        [btn setTitle:@"开始" forState:UIControlStateNormal];        btn.backgroundColor = [UIColor greenColor];        [imgView2 stopAnimating];        btn.tag = 2;    }else{        [btn setTitle:@"停止" forState:UIControlStateNormal];        btn.backgroundColor = [UIColor grayColor];        [imgView2 startAnimating];        btn.tag = 1;    }    NSLog(@"OMG,it is %i",(int)btn.tag);}


0 0