美文网首页iOS暑假小学期实训
IOS暑假小学期实训 第3天 “UI按钮交互功能”

IOS暑假小学期实训 第3天 “UI按钮交互功能”

作者: Just_smile | 来源:发表于2016-07-13 13:15 被阅读0次

    2016/07/11

    //

    //  ViewController.m

    //  UI按钮交互功能

    //

    //  Created by lanou on 16/7/11.

    //  Copyright © 2016年yhy. All rights reserved.

    //

    #import "ViewController.h"

    @interface ViewController ()

    //标题标签

    @property(nonatomic,strong)UILabel *titleLable;

    //左边按钮

    @property(nonatomic,strong)UIButton *leftBtn;

    //右边按钮

    @property(nonatomic,strong)UIButton *rightBtn;

    //显示图片

    @property(nonatomic,strong)UIImageView *myImageView;

    //给存入的四张图片定义一个数组

    @property(nonatomic,strong)NSArray *imageNames;

    @end

    @implementation ViewController

    //视图加载好时会自动调用

    - (void)viewDidLoad {

    [super viewDidLoad];

    //创建并初始化标签

    self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 150, 30)];

    //写入标签文本

    self.titleLable.text = @"biaoqingdi";

    //设置标签文本的背景颜色为红色

    self.titleLable.backgroundColor = [UIColor greenColor];

    //设置标签文本在框中居中对齐

    self.titleLable.textAlignment = NSTextAlignmentCenter;

    //添加标签到视图上

    [self.view addSubview:self.titleLable];

    //创建并初始化定义(坐标、宽高)相框

    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(85, 100, 200, 200)];

    //将图片保存在名为image中(根据图片地址名加载图片)

    UIImage *image = [UIImage imageNamed:@"biaoqingdi"];

    //显示图片

    self.myImageView.image = image;

    //添加相框到视图中

    [self.view addSubview:_myImageView];

    //创建并初始化定义(坐标、宽高)左按钮

    self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];

    //关闭左按钮的交互

    self.leftBtn.userInteractionEnabled = NO;

    //将左按钮的图片保存在名为leftImage中(根据图片地址名加载图片)

    UIImage *leftImage = [UIImage imageNamed: @"left_disable"];

    //设置左按钮的背景图片(在正常状态下)

    [self.leftBtn setBackgroundImage:leftImage forState:(UIControlStateNormal)];

    //添加左按钮到视图上

    [self.view addSubview: _leftBtn];

    //创建并初始化定义(坐标、宽高)右按钮

    self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];

    //将右按钮的图片保存在名为rightImage中(根据图片地址名加载图片)

    UIImage *rightImage = [UIImage imageNamed: @"right_normal"];

    //设置右按钮的背景图片(在正常状态下)

    [self.rightBtn setBackgroundImage:rightImage forState:(UIControlStateNormal)];

    //添加右按钮在视图上

    [self.view addSubview:_rightBtn];

    //右按钮监听

    [self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

    //左按钮监听

    [self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

    }

    //右按钮

    -(void)rightBtnAction{

    //切换图片   图片为png格式则不用加后缀名 其他格式需要加后缀

    self.imageNames = @[ @"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

    //获取当前是第几张图片(整型变量)

    NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];

    //把按钮图片加载成灰色(正常状态下状态)

    //不是为最后一张才切换到下一张

    if (index < 4) {

    //如果index=3(即图片从第四张切换到,第五张时)

    if (index == 3) {

    //关闭右按钮交互(右按钮图片变为灰色)

    self.rightBtn.userInteractionEnabled = NO;

    //将右按钮灰色图片保存在image中(根据名字加载图片)

    UIImage *image =[UIImage imageNamed:@"right_disable"];

    //将右按钮状态为一般(正常)状态下的背景图片设置成为image中图像

    [self.rightBtn setBackgroundImage:image forState:(UIControlStateNormal)];

    }

    else{

    //否则(图片为第1,2,3张时)

    //将两个按钮正常状态下的背景图片设置为黑色图片

    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:(UIControlStateNormal)];

    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:(UIControlStateNormal)];

    //开启两个按钮的交互

    self.rightBtn.userInteractionEnabled = YES;

    self.leftBtn.userInteractionEnabled = YES;

    }

    //将标签titleLable内容切换到下一张图片的标签内容

    NSString *nextTitle = self.imageNames[index+1];

    self.titleLable.text = nextTitle;

    //将图片切换到下一张

    self.myImageView.image = [UIImage imageNamed: nextTitle];

    }

    }

    //左按钮

    -(void)leftBtnAction{

    //切换图片   图片为png格式则不用加后缀名 其他格式需要加后缀

    self.imageNames = @[ @"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

    //获取当前是第几张图片(整型)

    NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];

    //不是为第一张才切换到上一张

    if (index > 0) {

    //第二张切换到第一张时

    if (index == 1) {

    //左边按钮交互关闭(左按钮图片变为灰色)

    self.leftBtn.userInteractionEnabled = NO;

    //将左按钮灰色图片保存在image中(根据名字加载图片)

    UIImage *image =[UIImage imageNamed:@"left_disable"];

    //将左按钮状态为一般(正常)状态下的背景图片设置成为image中图像

    [self.leftBtn setBackgroundImage:image forState:(UIControlStateNormal)];

    }

    else{

    //否则(图片为第3,4,5张时)

    //将两个按钮正常状态下的背景图片设置为黑色图片

    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:(UIControlStateNormal)];

    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:(UIControlStateNormal)];

    //开启两个按钮的交互

    self.rightBtn.userInteractionEnabled = YES;

    self.leftBtn.userInteractionEnabled = YES;

    }

    //将标签titleLable内容切换到上一张图片的标签内容

    NSString *preTitle =self.imageNames[index-1];

    self.titleLable.text = preTitle;

    //将图片切换到上一张

    self.myImageView.image = [UIImage imageNamed: preTitle];

    }

    }

    -(void)demo{

    //按钮UIButton

    //    UIButton *button = [UIButton buttonWithType: UIButtonTypeContactAdd];

    //创建并初始化定义(坐标、宽高)按钮

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

    //      frame表明了空间的坐标和宽高(CGRect类型)

    [button setTitle:@"眼镜哥" forState:UIControlEventTouchDown];

    //设置按钮背景色为红色

    button.backgroundColor = [UIColor redColor];

    //根据名字加载图片

    UIImage *image = [UIImage imageNamed:@"right_normal"];

    //给按钮设置背景图片

    [button setBackgroundImage:image forState:UIControlStateNormal];

    //按钮的监听

    [button addTarget:self action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpOutside];

    //添加到屏幕上面

    [self.view addSubview:button];

    //相框UIImageView

    //创建并初始化视图相框

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 50, 200, 200)];

    UIImage *image1 = [UIImage imageNamed:@"biaoqingdi"];

    //设置imageView显示的图片

    imageView.image = image1;

    [self.view addSubview:imageView];

    //标签UIlable

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 270, 150, 30)];

    //设置标签的文本

    label.text = @"表情弟";

    //设置居中方式(注:是将文本在标签框内部居中)

    label.textAlignment = NSTextAlignmentCenter;

    //设置标签的颜色为红色

    label.textColor = [UIColor redColor];

    //将标签显示在视图屏幕上

    [self.view addSubview:label];

    }

    //方法调用

    - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    }

    //调用按钮监听的方法

    -(void)btnClickLister{

    //在视图屏幕中执行按钮操作时,代码工程中将会打印“click btn”

    NSLog(@"click btn");

    }

    @end


    相关文章

      网友评论

        本文标题:IOS暑假小学期实训 第3天 “UI按钮交互功能”

        本文链接:https://www.haomeiwen.com/subject/jfwsjttx.html