您的位置:首页 > 产品设计 > UI/UE

UI中常用的控件ImageView\Button

2015-03-24 19:28 369 查看
- (void)viewDidLoad {

    [superviewDidLoad];

    //创建显示图片的控件
   UIImageView *imgView = [[UIImageViewalloc]
initWithFrame:CGRectMake(90,90,
200, 200)];

    imgView.backgroundColor = [UIColorredColor];

    //创建一个img,图片名后缀默认就是png,如果后缀不是png,一定要加上
   UIImage *img = [UIImageimageNamed:@"1"];

    
   UIImage *img2 = [UIImageimageNamed:@"scene1.jpg"];

    

    //给一个图片显示
    imgView.image = img;

    //设置高亮状态显示的图片
    imgView.highlightedImage = img2;

    

    //让图片处于高亮状态
    imgView.highlighted =YES;

    

    //设置图片的填充方式,默认是拉伸显示,图片会失真

//    UIViewContentModeScaleAspectFit 
等比例显示

    imgView.contentMode =UIViewContentModeScaleAspectFit;

    
    [self.viewaddSubview:imgView];

    

    //创建imgView
   UIImageView *imgView1 = [[UIImageViewalloc]
initWithFrame:CGRectMake(90,350,
150, 200)];

    imgView1.backgroundColor = [UIColorgreenColor];
    [self.viewaddSubview:imgView1];

    

    NSMutableArray *imgViews = [[NSMutableArrayalloc]
init];

    
   for (int i=1; i<18; i++) {

        //设置图片的名字
       NSString *imgName = [NSStringstringWithFormat:@"campFire%02d.gif",i];

       //创建img
       UIImage *img = [UIImageimageNamed:imgName];
        [imgViewsaddObject:img];
    }

    

    //设置动画集合
    imgView1.animationImages = imgViews;

    

    //设置动画时间

    [imgView1 setAnimationDuration:1];

    

    //开始动画
    [imgView1startAnimating];

    

    //停止动画

//    [imgView1 stopAnimating];

    
    [imgView1release];

    

    
}

//Button

- (void)viewDidLoad {

    [superviewDidLoad];

    [self_initButton];

    
}

- (void)_initButton {

    //创建按钮

//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(90, 90, 90, 50)];

//    //设置按钮的类型(无法设置按钮的类型)

//    button.buttonType = UIButtonTypeCustom;

    

    //使用类方法

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
    button.frame =CGRectMake(90,90,
90, 50);

    button.backgroundColor = [UIColororangeColor];

    

    //按钮设置标题:设置标题和状态绑定

    //正常状态的时候显示的标题(UIControlStateNormal)

    [button setTitle:@"按钮"forState:UIControlStateNormal];

    //按钮被点击的时候显示的标题(UIControlStateHighlighted)

    [button setTitle:@"点我了"forState:UIControlStateHighlighted];

    

    //设置标题的颜色:和状态绑定

    [button setTitleColor:[UIColorpurpleColor]
forState:UIControlStateNormal];

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

    

    //设置字体的大小:不需要绑定状态
    button.titleLabel.font = [UIFontsystemFontOfSize:20];

    

    //设置tag
    button.tag =1;

    

    //添加点击事件
   /*

     UIControlEventTouchDown:按下的时候就执行

     UIControlEventTouchDownRepeat: 多次点击的时候执行

     UIControlEventTouchDragInside:按着拖拽的时候会不停的执行

     UIControlEventTouchUpInside:按钮被点击松开后执行

     */

    [button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
    [self.viewaddSubview:button];

    

    //使用系统默认的样式

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
    button2.tag =2;
    button2.frame =CGRectMake(100,250,
40, 40);

    [button2 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
    [self.viewaddSubview:button2];

    
}

//按钮点击的方法
- (void)buttonAction:(UIButton *)button {

   if (button.tag ==1) {
       NSLog(@"1按钮被点击了");
    }elseif (button.tag ==2) {
       NSLog(@"2按钮被点击了");
    }

    

    

    
}

- (void)viewDidLoad {

    [super
viewDidLoad];

    [self
_initCustomButton];

    
}

//自定义按钮的使用
- (void)_initCustomButton {

    //创建一个按钮

    UIButton *button = [UIButton
buttonWithType:UIButtonTypeCustom];
    button.frame =
CGRectMake(90,
90, 90,
40);

    

    //设置显示的图片(使用这种方式无法显示标题)
   
UIImage *img1 = [UIImage
imageNamed:@"back_on_black"];

//    [button setImage:img1 forState:UIControlStateNormal];

    //设置点击的时候的显示的图片
   
UIImage *img2 = [UIImage
imageNamed:@"back_on"];

//    [button setImage:img2 forState:UIControlStateHighlighted];

    

    //如果想显示图片的同时显示标题

    [button setBackgroundImage:img1
forState:UIControlStateNormal];

    [button setBackgroundImage:img2
forState:UIControlStateHighlighted];

    

    //设置标题

    [button setTitle:@"按钮"
forState:UIControlStateNormal];

    

    

    //添加点击事件

    [button addTarget:self
action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];

    

    //按钮被禁用的时候显示的标题

    [button setTitle:@"售罄"
forState:UIControlStateDisabled];

   

    //禁用按钮,图片的颜色会变浅UIControlStateDisabled
    button.enabled =
NO;

    

    //按钮被禁用了,但是图片不变

//    button.userInteractionEnabled = NO;

    
    [self.view
addSubview:button];

    

    UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeCustom];
    button1.frame =
CGRectMake(100,
250, 100,
100);

    [button1 setImage:[UIImage
imageNamed:@"scene1.jpg"]
forState:UIControlStateNormal];

    [button1 setImage:[UIImage
imageNamed:@"scene2.jpg"]
forState:UIControlStateSelected];

    

    [button1 addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];

    
    [self.view
addSubview:button1];

    
}

- (void)buttonAction:(UIButton *)button {

   
NSLog(@"哈哈");
    button.selected = !button.selected;

    
}

- (void)buttonAction {

    NSLog(@"按钮被点击了");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐