代码 创建按钮

来源:互联网 发布:远程数据采集系统 编辑:程序博客网 时间:2024/05/22 05:09

——————————————————————————————


//视图加载完毕后就会自动调用(系统自动调用),一般会在这个方法中进行初始化控件

- (void)viewDidLoad {

  [superviewDidLoad];


  NSLog(@"视图加载完毕调用");

  //    1.创建按钮对象

 UIButton *head = [[UIButtonalloc] init];

  //    2.设置frame

  head.frame =CGRectMake(30,60, 120, 120);

  //    3.添加

  [self.viewaddSubview:head];

  //    4.设置背景图片

 UIImage *nomalImage = [UIImageimageNamed:@"btn_01"];

  //     4.1设置普通状态的图片

  [head setBackgroundImage:nomalImageforState:UIControlStateNormal];

  //     4.2设置高亮状态的图片

 UIImage *highImage = [UIImageimageNamed:@"btn_02"];


  [head setBackgroundImage:highImageforState:UIControlStateHighlighted];

  //    5.设置文字

  [head setTitle:@"摸我吧"forState:UIControlStateNormal];

  [head setTitle:@"摸我干啥"forState:UIControlStateHighlighted];

  //    6.设置文字颜色

  [head setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

  [head setTitleColor:[UIColorredColor] forState:UIControlStateHighlighted];

  //    7.创建一个加号按钮

  //     7.1创建加号按钮


  UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];

  [self.view addSubview:addBtn];


  //    8.给加号添加点击事件

  //    addTarget:表示监听者:控制器来监听

  //    forControlEvents:监听到某个事件

  //    action:表示监听者监听到某个事件后调用action这个方法做些事情

  //    [addBtn addTarget:self action:@selector(addBtnClick)

  //    forControlEvents:UIControlEventTouchUpInside];

  //    9.给头像添加点击事件

  //    [head addTarget:self action:@selector(headBtnClick)

  //    forControlEvents:UIControlEventTouchUpInside];


  //    10.一个方法监听两个按钮点击

  [head addTarget:self

                action:@selector(btnClick:)

      forControlEvents:UIControlEventTouchUpInside];

  [addBtn addTarget:self

                action:@selector(btnClick:)

      forControlEvents:UIControlEventTouchUpInside];

}


//隐藏状态栏

- ($BOOL)prefersStatusBarHidden {


  return YES;

}


// 监听头像和加号按钮点击后调用的方法


- (void)btnClick:(UIButton *)btn {

  //    NSLog(@"监听到两个按钮的点击");

  NSLog(@"%@", btn);

}



———————————字典———————————————————


- (void)viewDidLoad {

    [superviewDidLoad];

    

//    加载数据

    

//    NSMutableDictionary  *dict1 = [NSMutableDictionary dictionary];

//    dict1[icon] = @"biaoqingdi";

//    dict1[desc] = @"在他面前,其他神马表情都弱爆了";

//    NSMutableDictionary  *dict2 = [NSMutableDictionary dictionary];

//    dict2[icon] = @"wangba";

//    dict2[desc] = @"哥们为什么选八号呢";

//    NSMutableDictionary  *dict3 = [NSMutableDictionary dictionary];

//    dict3[icon] = @"bingli";

//    dict3[desc] = @"这也忒狠了";

//    NSMutableDictionary  *dict4 = [NSMutableDictionary dictionary];

//    dict4[icon] = @"chiniupa";

//    dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";

//    NSMutableDictionary  *dict5 = [NSMutableDictionary dictionary];

//    dict5[icon] = @"danteng";

//    dict5[desc] = @"亲,你能改下你的网名么?哈哈";

//    NSMutableDictionary  *dict6 = [NSMutableDictionary dictionary];

//    dict6[icon] = @"danteng";

//    dict6[desc] = @"亲,你能改下你的网名么?哈哈";

//

//    

//    _images = @[dict1,dict2,dict3,dict4,dict5,dict6];

//    

    

    

    

    


    [selfchangData];

    

//    self.noLabel.text = [NSString stringWithFormat:@"1/5"];

//    self.headView.image = [UIImage imageNamed:@"biaoqingdi"];

//    self.descLabel.text = @"在他面前,其他神马表情都弱爆了!";

//    self.previousBtn.enabled = NO;

    

    

    

    

   


}


//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据

//好处:提高性能

- (NSArray *)images{

   if (_images ==nil) {

        NSMutableDictionary  *dict1 = [NSMutableDictionarydictionary];

        dict1[icon] =@"biaoqingdi";

        dict1[desc] =@"在他面前,其他神马表情都弱爆了";

        NSMutableDictionary  *dict2 = [NSMutableDictionarydictionary];

        dict2[icon] =@"wangba";

        dict2[desc] =@"哥们为什么选八号呢";

        NSMutableDictionary  *dict3 = [NSMutableDictionarydictionary];

        dict3[icon] =@"bingli";

        dict3[desc] =@"这也忒狠了";

        NSMutableDictionary  *dict4 = [NSMutableDictionarydictionary];

        dict4[icon] =@"chiniupa";

        dict4[desc] =@"这小姑娘吃个牛排比杀牛还费劲啊";

        NSMutableDictionary  *dict5 = [NSMutableDictionarydictionary];

        dict5[icon] =@"danteng";

        dict5[desc] =@"亲,你能改下你的网名么?哈哈";

        

        

       _images = @[dict1,dict2,dict3,dict4,dict5];


    }

    return_images;

    

}



//

- (IBAction)prvious {

    

//    1.索引值减一

   self.index --;

//    2.根据索引值设置数据

    [selfchangData];



    

}


//next按钮点击的方法

- (IBAction)next {

//    1.索引值加一

//    self.index = self.index +1;

   self.index ++;

//    2.根据索引值设置数据

    [selfchangData];

    

    

    

    

    

    

}

//改变控件的数据


- (void)changData{

   self.noLabel.text = [NSStringstringWithFormat:@"%d/%ld",self.index +1,self.images.count];

    

      NSDictionary *dict =  self.images[self.index];

   self.headView.image = [UIImageimageNamed:dict[icon]];

   self.descLabel.text = dict[desc];

  

    

    

    

            

            

            

            

            


    //    3.设置按钮状态

    

    //          3.1左边按钮设置状态

    

   self.previousBtn.enabled = (self.index !=0);

    

    

    

    //        3.2右边按钮设置状态

   self.nextBtn.enabled = (self.index != (self.images.count - 1));

    


}



——————————plist————————————————————


- (void)viewDidLoad {

  [superviewDidLoad];


 

  [selfchangData];


  //    self.noLabel.text = [NSString stringWithFormat:@"1/5"];

  //    self.headView.image = [UIImage imageNamed:@"biaoqingdi"];

  //    self.descLabel.text = @"在他面前,其他神马表情都弱爆了!";

  //    self.previousBtn.enabled = NO;

}


//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据

//好处:提高性能

- (NSArray *)images {

 if (_images ==nil) {

    //        1.获取全路径

   NSString *path =

        [[NSBundlemainBundle] pathForResource:@"images"ofType:@"plist"];

    //        arrayWithContentsOfFile:必须传入全路径(有路径必须传入全路径)

    //        2.读取plist文件


    _images = [NSArrayarrayWithContentsOfFile:path];

  }

  return_images;

}


//

- (IBAction)prvious {


  //    1.索引值减一

 self.index--;

  //    2.根据索引值设置数据

  [selfchangData];

}


// next按钮点击的方法

- (IBAction)next {

  //    1.索引值加一

  //    self.index = self.index +1;

 self.index++;

  //    2.根据索引值设置数据

  [selfchangData];

}

//改变控件的数据


- (void)changData {

  self.noLabel.text =

      [NSStringstringWithFormat:@"%d/%ld",self.index +1, self.images.count];


 NSDictionary *dict = self.images[self.index];

 self.headView.image = [UIImageimageNamed:dict[icon]];

 self.descLabel.text = dict[desc];


  //    3.设置按钮状态


  //          3.1左边按钮设置状态


  self.previousBtn.enabled = (self.index != 0);


  //        3.2右边按钮设置状态

 self.nextBtn.enabled = (self.index != (self.images.count - 1));

}



—————————模型—————————————————————


- (void)viewDidLoad {

  [superviewDidLoad];


  [selfchangData];


  }


//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据

//好处:提高性能

- (NSArray *)images {

 if (_images ==nil) {

    //        1.获取全路径

    //    NSBundle:代表了一个资源包文件夹

    //        NSString *path =[[NSBundle mainBundle]pathForResource:@"images"

    //        ofType:@"plist"];

   //

   NSString *path =

        [[NSBundlemainBundle] pathForResource:@"images.plist"

                                       ofType:nil];//第二种获取全路径的方法.

                                                    //        arrayWithContentsOfFile:必须传入全路径(有路径必须传入全路径)

    //        2.读取plist文件


   NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];

    //        3.字典转模型

    NSMutableArray *tempArray = [NSMutableArrayarray];


   for (NSDictionary *dictin dictArray) {

     ImageModel *imageModel = [[ImageModelalloc] init];


      imageModel.icon = dict[@"icon"];

      imageModel.desc = dict[@"desc"];

      [tempArrayaddObject:imageModel];

    }

   _images = tempArray;

  }

  return_images;

}


//

- (IBAction)prvious {


  //    1.索引值减一

 self.index--;

  //    2.根据索引值设置数据

  [selfchangData];

}


// next按钮点击的方法

- (IBAction)next {

  //    1.索引值加一

  //    self.index = self.index +1;

 self.index++;

  //    2.根据索引值设置数据

  [self changData];

}

//改变控件的数据


- (void)changData {

 self.noLabel.text =

      [NSString stringWithFormat:@"%d/%ld",self.index + 1,self.images.count];


  //       NSDictionary *dict =  self.images[self.index];

  //    self.headView.image = [UIImage imageNamed:dict[icon]];

  //    self.descLabel.text = dict[desc];

  ImageModel *imageMode =self.images[self.index];


 self.headView.image = [UIImage imageNamed:imageMode.icon];

 self.descLabel.text = imageMode.desc;


  //    3.设置按钮状态


  //          3.1左边按钮设置状态


 self.previousBtn.enabled = (self.index !=0);


  //        3.2右边按钮设置状态

 self.nextBtn.enabled = (self.index != (self.images.count -1));

}






——————————————————————————————
oc strong     ui  weak/ strong
——————————序列帧动画————————————————————

}

//执行和牛奶的动画

- (IBAction)drink {


  [selfrunAnimationWithName:@"drink"andCount:81];

}

- (IBAction)knock {

  [selfrunAnimationWithName:@"knockout"andCount:81];

}


//执行动画:重构原则:把相同的代码放在一起,把不同的东西作为参数

- (void)runAnimationWithName:(NSString *)imageName andCount:(int)count {

  //    0.判断动画是否在执行,然后确定是否继续执行程序

  if (self.Tom.isAnimating) {

    return;

  }


  //    1.获取播放动画的图片

  NSMutableArray *images = [NSMutableArrayarray];


 for (int i =0; i < count; i++) {


    //        1.1拼接图片的名称(2是表示去两位,0是表示占位符,不足两位补0)

   NSString *name = [NSStringstringWithFormat:@"%@_%02d.jpg", imageName, i];

    //          imageNamed:加载图片,图片加载完成后图片会驻留内存,好处:下次加载图片的时候会很快,不好处:占用内存空间

    //        imageWithContentsOfFile:加载出来的图片不会驻留内存,


    //        UIImage *image = [UIImage imageNamed:name];

    NSString *path = [[NSBundlemainBundle] pathForResource:nameofType:nil];

   UIImage *image = [UIImageimageWithContentsOfFile:path];


    [imagesaddObject:image];

  }

  //    2.告诉tom播放哪些图片

  self.Tom.animationImages = images;


  //     2.1设置播放的次数

  self.Tom.animationRepeatCount =1;

  //     2.2设置播放的时间

 CGFloat delat = 0.08;//播放每一张图片的时间

 self.Tom.animationDuration = delat * images.count;


  //    3.播放动画

  [self.TomstartAnimating];


  //     4.清空图片的缓存

  //    performSelector:执行方法

  //    withObject:方法参数

  //    afterDelay:多长时间之后

  //    self afterDelay时间后执行performSelector这个方法


  [selfperformSelector:@selector(clearCache)

            withObject:nil

            afterDelay:delat * images.count];

}


- (void)clearCache {

  self.Tom.animationImages =nil;

}



———————————sound———————————————————


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface ViewController ()

- (IBAction)drink;

@property(weak, nonatomic) IBOutletUIImageView *Tom;

- (IBAction)knock;


@property(nonatomic,strong) AVAudioPlayer *play;


@end


@implementation ViewController



//执行和牛奶的动画

- (IBAction)drink {


  [selfrunAnimationWithName:@"drink"andCount:81];

  //    [self playSound];$

  [selfperformSelector:@selector(playSound)withObject:nilafterDelay:2.5];

}

- (IBAction)knock {

  [selfrunAnimationWithName:@"knockout"andCount:81];

}


//执行动画:重构原则:把相同的代码放在一起,把不同的东西作为参数

- (void)runAnimationWithName:(NSString *)imageName andCount:(int)count {

  //    0.判断动画是否在执行,然后确定是否继续执行程序

  if (self.Tom.isAnimating) {

    return;

  }


  //    1.获取播放动画的图片

  NSMutableArray *images = [NSMutableArrayarray];


 for (int i =0; i < count; i++) {


    //        1.1拼接图片的名称(2是表示去两位,0是表示占位符,不足两位补0)

   NSString *name = [NSStringstringWithFormat:@"%@_%02d.jpg", imageName, i];

    //          imageNamed:加载图片,图片加载完成后图片会驻留内存,好处:下次加载图片的时候会很快,不好处:占用内存空间

    //        imageWithContentsOfFile:加载出来的图片不会驻留内存,


    //        UIImage *image = [UIImage imageNamed:name];

    NSString *path = [[NSBundlemainBundle] pathForResource:nameofType:nil];

   UIImage *image = [UIImageimageWithContentsOfFile:path];


    [imagesaddObject:image];

  }

  //    2.告诉tom播放哪些图片

  self.Tom.animationImages = images;


  //     2.1设置播放的次数

  self.Tom.animationRepeatCount =1;

  //     2.2设置播放的时间

 CGFloat delat = 0.08;//播放每一张图片的时间

 self.Tom.animationDuration = delat * images.count;


  //    3.播放动画

  [self.TomstartAnimating];


  //     4.清空图片的缓存

  //    performSelector:执行方法

  //    withObject:方法参数

  //    afterDelay:多长时间之后

  //    self afterDelay时间后执行performSelector这个方法


  [selfperformSelector:@selector(clearCache)

            withObject:nil

            afterDelay:delat * images.count];

}


- (void)clearCache {

  self.Tom.animationImages =nil;

}


- (void)playSound {


  //    1.获取音效的路径


 NSString *path =

      [[NSBundlemainBundle] pathForResource:@"p_drink_milk"ofType:@"wav"];


  //    2.把路径转化成url

 NSURL *url = [NSURLfileURLWithPath:path];


  //    2.创建一个播放器

 NSError *error = nil;

  //    3.创建一个播放器

  AVAudioPlayer *play =

      [[AVAudioPlayeralloc] initWithContentsOfURL:urlerror:&error];

 self.play = play;

  //    4.播放

  [self.playplay];

}



——————————模型————————————————————


- (id)initWithDict:(NSDictionary *)dict{

   if (self = [superinit]) {

        

        

       self.icon = dict[@"icon"];

       self.name = dict[@"name"];

              


    }

    

    return self;

    

    

  

    

}


+ (id)appWithDict:(NSDictionary *)dict{

    

    

   return  [[selfalloc]initWithDict:dict];

    

}


#import "AppModel.h"


@interface ViewController ()

@property(nonatomic,strong) NSArray *apps;


@end


@implementation ViewController


//懒加载

- (NSArray *)apps {


 if (_apps ==nil) {

    //        1.获取plist全路径

   NSString *path =

        [[NSBundlemainBundle] pathForResource:@"app"ofType:@"plist"];

    //        2.读取plist文件

   NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];

    //        3.字典转模型

    NSMutableArray *tempArray = [NSMutableArrayarray];

   for (NSDictionary *dictin dictArray) {

      //            AppModel *appModel = [[AppModel alloc]init];

      //            appModel.icon = dict[@"icon"];

      //            appModel.name = dict[@"name"];

     AppModel *appModel = [AppModelappWithDict:dict];


      [tempArrayaddObject:appModel];

    }

   _apps = tempArray;

  }

  return _apps;

}


- (void)viewDidLoad {

  [superviewDidLoad];

  // Do any additional setup after loading the view, typically from a nib.

  //    1.设置格子的相关的数据

 int totalColumn = 3;

 CGFloat appViewW = 100;

 CGFloat appViewH = 120;

 CGFloat spaceXmargin = 15;

  CGFloat viewW = self.view.frame.size.width;

 CGFloat letfXmargin =

      (viewW - totalColumn * appViewW - (totalColumn -1) * spaceXmargin) * 0.5;

 CGFloat topMargn = 50;

 CGFloat spaceYmargin = 20;


  //    2.创建格子

 for (int i =0; i < self.apps.count; i++) {

    //    创建第一个格子

    //    2.1创建一个格子

   UIView *appView = [[UIViewalloc] init];


   int col = i % totalColumn;

   int row = i / totalColumn;

   CGFloat appViewX = letfXmargin + (appViewW + spaceXmargin) * col;

   CGFloat appViewY = topMargn + (appViewH + spaceYmargin) * row;


    //    2.2设置frame

    appView.frame =CGRectMake(appViewX, appViewY, appViewW, appViewH);

    //    2.3添加

    [self.viewaddSubview:appView];

    //    2.4设置背景色

    //        appView.backgroundColor  = [UIColor redColor];


    //取出数据

   AppModel *appModel = self.apps[i];


    //    2.5添加一个图片

    //    2.5.1 创建一个imageView


   UIImageView *headView = [[UIImageViewalloc] init];

   CGFloat headViewY = 0;

   CGFloat headViewW = 60;

   CGFloat headViewX = (appViewW - headViewW) * 0.5;

   CGFloat headViewH = 60;

    //    2.5.2设置frame


    headView.frame =CGRectMake(headViewX, headViewY, headViewW, headViewH);

    //            2.5.3 添加

    [appViewaddSubview:headView];

    //        headView.backgroundColor = [UIColor blueColor];


    //    2.5.4设置图片数据

    headView.image = [UIImageimageNamed:appModel.icon];


    //    2.6添加label

    //    2.6.1创建一个label

   UILabel *nameLabel = [[UILabelalloc] init];

   CGFloat nameLabelX = 0;

   CGFloat nameLabelW = appViewW;

   CGFloat nameLabelY = headViewY + headViewH;

   CGFloat nameLabelH = 20;

    //    2.6.2设置frame

    nameLabel.frame =

       CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);

    //    2.6.3 添加

    [appViewaddSubview:nameLabel];

    //        nameLabel.backgroundColor = [UIColor greenColor];

    //    2.6.4 设置文字数据

    nameLabel.text = appModel.name;

    //    2.6.5 设置文字的大小

    nameLabel.font = [UIFontsystemFontOfSize:14];

    //    2.6.6 文字居中

    nameLabel.textAlignment =NSTextAlignmentCenter;


    //    2.7添加按钮


   //

   UIButton *downLoadBtn = [[UIButtonalloc] init];

   CGFloat downLoadBtnX = 10;

   CGFloat downLoadBtnY = nameLabelY + nameLabelH +10;

   CGFloat downLoadBtnW = appViewW - 2 * downLoadBtnX;

   CGFloat downLoadBtnH = 25;


    downLoadBtn.frame =

       CGRectMake(downLoadBtnX, downLoadBtnY, downLoadBtnW, downLoadBtnH);

    [appViewaddSubview:downLoadBtn];

    //        downLoadBtn.backgroundColor = [UIColor grayColor];

    //   2.7.1设置图片

    [downLoadBtn setBackgroundImage:[UIImageimageNamed:@"buttongreen"]

                          forState:UIControlStateNormal];

    [downLoadBtn

        setBackgroundImage:[UIImageimageNamed:@"buttongreen_highlighted"]

                  forState:UIControlStateHighlighted];

    //   2.7.2设置按钮文字

    [downLoadBtn setTitle:@"下载"forState:UIControlStateNormal];

    //        按钮内部至少有两个内置的控件,一个ImageView,

    //        用来显示图片,一个label,用来显示文字.

    downLoadBtn.titleLabel.font = [UIFontsystemFontOfSize:14];

  }

}





———————————xib———————————————————

#import "AppModel.h"


@interface ViewController ()

@property(nonatomic,strong) NSArray *apps;


@end


@implementation ViewController


//懒加载

- (NSArray *)apps {


 if (_apps ==nil) {

    //        1.获取plist全路径

   NSString *path =

        [[NSBundlemainBundle] pathForResource:@"app"ofType:@"plist"];

    //        2.读取plist文件

   NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];

    //        3.字典转模型

    NSMutableArray *tempArray = [NSMutableArrayarray];

   for (NSDictionary *dictin dictArray) {

      //            AppModel *appModel = [[AppModel alloc]init];

      //            appModel.icon = dict[@"icon"];

      //            appModel.name = dict[@"name"];

     AppModel *appModel = [AppModelappWithDict:dict];


      [tempArrayaddObject:appModel];

    }

   _apps = tempArray;

  }

  return _apps;

}


- (void)viewDidLoad {

  [superviewDidLoad];

  // Do any additional setup after loading the view, typically from a nib.

  //    1.设置格子的相关的数据

 int totalColumn = 3;

 CGFloat appViewW = 100;

 CGFloat appViewH = 120;

 CGFloat spaceXmargin = 15;

  CGFloat viewW = self.view.frame.size.width;

 CGFloat letfXmargin =

      (viewW - totalColumn * appViewW - (totalColumn -1) * spaceXmargin) * 0.5;

 CGFloat topMargn = 50;

 CGFloat spaceYmargin = 20;


  //    2.创建格子

 for (int i =0; i < self.apps.count; i++) {

    //    创建第一个格子

    //    2.1创建一个格子

   UIView *appView = [[[NSBundlemainBundle] loadNibNamed:@"AppView"

                                                    owner:nil

                                                  options:nil]lastObject];


   int col = i % totalColumn;

   int row = i / totalColumn;

   CGFloat appViewX = letfXmargin + (appViewW + spaceXmargin) * col;

   CGFloat appViewY = topMargn + (appViewH + spaceYmargin) * row;

    appView.frame =CGRectMake(appViewX, appViewY, appViewW, appViewH);

    [self.viewaddSubview:appView];

    //   2.2给格子中的子控件设置数据,可以通过控件的父控件的一个方法根据tag值取得对应的控件(第一种去得子控件的方法)

   AppModel *appModel = self.apps[i];


    //   2.3通过tag值获取控件

    //       UIImageView *headView = (UIImageView *) [appView viewWithTag:10];

   //

    //        headView.image = [UIImage imageNamed: appModel.icon];

   //

    //        UILabel *nameLabel = (UILabel *)[appView viewWithTag:20];

    //        nameLabel.text = appModel.name;

    //           2.4通过subView属性获取子控件


   UIImageView *headView = (UIImageView *)appView.subviews[0];

    headView.image = [UIImageimageNamed:appModel.icon];


   UILabel *nameLabel = (UILabel *)appView.subviews[1];

    nameLabel.text = appModel.name;

   //

  }

}


——————————————————————————————

——————————————————————————————




——————————————————————————————

——————————————————————————————

——————————————————————————————

——————————————————————————————


0 0