IOS_UI_day2_UIButton

来源:互联网 发布:淘宝激活店铺激活不了 编辑:程序博客网 时间:2024/05/16 08:44
H:/IOS_UI/day2-01-按钮操作-MJViewController.h
////  MJViewController.h//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJViewController : UIViewController@property (weak, nonatomic) IBOutlet UIButton *btn;- (IBAction)up:(id)sender;- (IBAction)down:(id)sender;- (IBAction)left:(id)sender;- (IBAction)right:(id)sender;@end

H:/IOS_UI/day2-01-按钮操作-MJViewController.m
////  MJViewController.m//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"@interface MJViewController ()@end@implementation MJViewController- (IBAction)up:(id)sender {    // OC语法规定:不允许直接修改 某个对象中结构体属性的成员        // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;    // 2.修改y值    tempFrame.origin.y -= 50;        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}- (IBAction)down:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;        // 2.修改y值    tempFrame.origin.y += 50;        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}- (IBAction)left:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;        // 2.修改y值    tempFrame.origin.x -= 50;        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}- (IBAction)right:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;        // 2.修改y值    tempFrame.origin.x += 50;        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}@end

H:/IOS_UI/day2-02-按钮操作-MJViewController.h
////  MJViewController.h//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJViewController : UIViewController@property (weak, nonatomic) IBOutlet UIButton *btn;// 行走- (IBAction)run:(id)sender;// 缩放- (IBAction)scale:(id)sender;// 旋转- (IBAction)rotate:(id)sender;@end

H:/IOS_UI/day2-02-按钮操作-MJViewController.m
////  MJViewController.m//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"#define kDelta 50//const int delta = 50;@interface MJViewController ()//{//    CGFloat _angle;//}@end@implementation MJViewController//- (void)begin//{//    // 0.动画(头部-开始动画)//    [UIView beginAnimations:nil context:nil];//    // 设置动画的执行时间//    [UIView setAnimationDuration:1.0];//}////- (void)end//{//    // 4.动画(尾部-提交动画-执行动画)//    [UIView commitAnimations];//}#pragma mark 控制按钮走动(上下左右)- (IBAction)run:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;        // 2.取出按钮的tag标记    int tag = [sender tag];    // CGFloat delta = 100;    switch (tag) {        case 1: // 上            tempFrame.origin.y -= kDelta;            break;                    case 2: // 右            tempFrame.origin.x += kDelta;            break;                    case  3: // 下            tempFrame.origin.y += kDelta;            break;                    case 4: // 左            tempFrame.origin.x -= kDelta;            break;                    default:            break;    }        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}#pragma mark 放大\缩小- (IBAction)scale:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.计算缩放比例    CGFloat scale = [sender tag] == 20 ? 1.2 : 0.8;    // 2.修改按钮形变属性    _btn.transform = CGAffineTransformScale(_btn.transform, scale, scale);        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}#pragma mark 左旋转\右旋转- (IBAction)rotate:(id)sender {//    _angle -= M_PI_4;        // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 弧度 3.14 - π    // 角度 180    // 向左旋转45°//    _btn.transform = CGAffineTransformMakeRotation(- M_PI_4);//    _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * (10 == tag?-1:1));        int tag = [sender tag];    if (10 == tag) { // 左        _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * -1);    } else { // 右        _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * 1);    }        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}@end

H:/IOS_UI/day2-03-按钮操作-MJViewController.h
////  MJViewController.h//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJViewController : UIViewController@property (weak, nonatomic) IBOutlet UIButton *btn;// 重置- (IBAction)reset:(id)sender;// 行走- (IBAction)run:(id)sender;// 缩放- (IBAction)scale:(id)sender;// 旋转- (IBAction)rotate:(id)sender;@end

H:/IOS_UI/day2-03-按钮操作-MJViewController.m
////  MJViewController.m//  01-按钮操作////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"#define kDelta 50//const int delta = 50;@interface MJViewController ()//{//    CGFloat _angle;//}@end@implementation MJViewController//- (void)begin//{//    // 0.动画(头部-开始动画)//    [UIView beginAnimations:nil context:nil];//    // 设置动画的执行时间//    [UIView setAnimationDuration:1.0];//}////- (void)end//{//    // 4.动画(尾部-提交动画-执行动画)//    [UIView commitAnimations];//}- (void)btnClickWithBlock:(void (^)())block{    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        block();        // 1.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}#pragma mark 控制按钮走动(上下左右)- (IBAction)run:(id)sender {    [self btnClickWithBlock:^{        // 1.先取出frame//        CGRect tempFrame = _btn.frame;        CGPoint tempCenter = _btn.center;                // 2.取出按钮的tag标记        int tag = [sender tag];        // CGFloat delta = 100;        switch (tag) {            case 1: // 上//                tempFrame.origin.y -= kDelta;                tempCenter.y -= kDelta;                break;                            case 2: // 右//                tempFrame.origin.x += kDelta;                tempCenter.x += kDelta;                break;                            case 3: // 下//                tempFrame.origin.y += kDelta;                tempCenter.y += kDelta;                break;                            case 4: // 左//                tempFrame.origin.x -= kDelta;                tempCenter.x -= kDelta;                break;                            default:                break;        }                // 3.重新赋值按钮的frame//        _btn.frame = tempFrame;        _btn.center = tempCenter;    }];}#pragma mark 放大\缩小- (IBAction)scale:(id)sender {    [self btnClickWithBlock:^{        CGFloat scale = [sender tag] == 20 ? 1.2 : 0.8;        _btn.transform = CGAffineTransformScale(_btn.transform, scale, scale);    }];}#pragma mark 左旋转\右旋转- (IBAction)rotate:(id)sender {//    _angle -= M_PI_4;        // 弧度 3.14 - π    // 角度 180    // 向左旋转45°//    _btn.transform = CGAffineTransformMakeRotation(- M_PI_4);//    _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * (10 == tag?-1:1));        [self btnClickWithBlock:^{        int tag = [sender tag];        if (10 == tag) { // 左            _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * -1);        } else { // 右            _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * 1);        }    }];}#pragma mark 重置- (IBAction)reset:(id)sender {    // 清空之前所有的形变状态(消除以前的旋转、缩放等状态)//    _btn.transform = CGAffineTransformIdentity;    [self btnClickWithBlock:^{        _btn.transform = CGAffineTransformIdentity;    }];}@end

H:/IOS_UI/day2-04-代码创建按钮-MJViewController.h
////  MJViewController.h//  02-代码创建按钮////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJViewController : UIViewController@end

H:/IOS_UI/day2-04-代码创建按钮-MJViewController.m
////  MJViewController.m//  02-代码创建按钮////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"@interface MJViewController ()@end@implementation MJViewController#pragma mark 控制器的view加载完毕的时候会调用一次- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.        // 1.创建按钮    // 1.1.创建    UIButton *btn = [[UIButton alloc] init];        NSLog(@"viewdidload----%p", btn);        // 1.2.设置按钮的尺寸和位置    btn.frame = CGRectMake(0, 0, 100, 100);        // 1.3.设置按钮普通状态下的属性    // 1.3.1.设置背景图片    UIImage *normal = [UIImage imageNamed:@"btn_01.png"];    [btn setBackgroundImage:normal forState:UIControlStateNormal];    // 1.3.2.设置文字    [btn setTitle:@"点我啊" forState:UIControlStateNormal];    // 1.3.3.设置文字颜色    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];        // 1.4.设置按钮高亮状态下的属性    // 1.4.1.设置背景图片    UIImage *high = [UIImage imageNamed:@"btn_02.png"];    [btn setBackgroundImage:high forState:UIControlStateHighlighted];    // 1.4.2.设置文字    [btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];    // 1.4.3.设置文字颜色    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];        // 1.5.监听按钮点击    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        // 2.添加按钮到控制器的view中    [self.view addSubview:btn];        // 3.添加文本输入框    UITextField *field = [[UITextField alloc] init];    field.frame = CGRectMake(100, 100, 100, 50);    field.backgroundColor = [UIColor redColor];        // 中点的x    CGFloat centerX = self.view.frame.size.width * 0.5;    CGFloat centerY = self.view.frame.size.height * 0.5;    field.center = CGPointMake(centerX, centerY);        // 设置字体    field.font = [UIFont systemFontOfSize:30];    //    [field setBackgroundColor:<#(UIColor *)#>]    [self.view addSubview:field];}#pragma mark 监听按钮点击- (void)btnClick:(UIButton *)btn{    }@end

H:/IOS_UI/day2-05-图片浏览器-descs.plist
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><string>在他面前,其他神马表情都弱爆了!</string><string>为什么你们选车牌,非得跟自己过不去呢?</string><string>下午一客户来维修电脑,当时哥打开电脑一开,就石化了</string><string>二逼青年伤不起啊,有木有啊啊啊啊</string><string>这也忒狠了</string><string>哥们为什么选八号呢</string><string>这年头的SB不少</string><string>够惨不?</string><string>亲,你能改下你的网名么?哈哈</string><string>这是在挑战小偷的智商么?</string><string>这两货一定是兄妹!</string><string>熊孩子又调皮了</string><string>这小姑娘吃个牛排比杀牛还费劲啊</string><string>我太TMD机智了</string><string>这是哪家电视台的,这么坑爹</string><string>求大神把我P得让人一见倾心的那种</string></array></plist>

H:/IOS_UI/day2-05-图片浏览器-MJViewController.h
////  MJViewController.h//  03-图片浏览器////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJViewController : UIViewController- (IBAction)nightMode:(UISwitch *)sender;- (IBAction)imageSizeChange:(UISlider *)sender;- (IBAction)setting;- (IBAction)sliderValueChange:(UISlider *)sender;@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UILabel *imageNo;@property (weak, nonatomic) IBOutlet UILabel *imageDesc;@property (weak, nonatomic) IBOutlet UIView *settingView;@end

H:/IOS_UI/day2-05-图片浏览器-MJViewController.m
////  MJViewController.m//  03-图片浏览器////  Created by apple on 13-11-22.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"@interface MJViewController (){    NSArray *_allDescs;}@end@implementation MJViewController#pragma mark 控制器的view加载完毕后会调用一次- (void)viewDidLoad{    [super viewDidLoad];        // 1.获得所有的描述(通过解析plist文件来创建数组对象,比如传入文件的全路径)    // 如果要访问项目中资源包里面的所有资源。应该用mainBundle    NSBundle *bundle = [NSBundle mainBundle];    // 获得文件的全路径    NSString *path = [bundle pathForResource:@"descs" ofType:@"plist"];    // 加载path对应的文件来创建数组     _allDescs = [NSArray arrayWithContentsOfFile:path];        // 2.设置默认的描述    _imageDesc.text = _allDescs[0];}#pragma mark 夜间模式- (IBAction)nightMode:(UISwitch *)sender {    if (sender.on) { // 开        self.view.backgroundColor = [UIColor darkGrayColor];    } else { // 关        self.view.backgroundColor = [UIColor whiteColor];    }}#pragma mark 图片尺寸改变了- (IBAction)imageSizeChange:(UISlider *)sender {//    // 1.取出frame//    CGRect tempFrame = _imageView.frame;//    //    // 2.修改frame//    tempFrame.size.width = sender.value * 320;//    tempFrame.size.height = sender.value * 100;//    //    // 3.重新赋值frame//    _imageView.frame = tempFrame;        _imageView.transform = CGAffineTransformMakeScale(sender.value, sender.value);}#pragma mark 点击了设置- (IBAction)setting {    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.5];        // 1.取出中点    CGPoint tempCenter = _settingView.center;        // 2.修改y值//    tempCenter.y -= _settingView.frame.size.height;        if (_settingView.frame.origin.y == self.view.frame.size.height) { // 设置界面目前看不见        tempCenter.y -= _settingView.bounds.size.height;    } else { // 能看见设置界面        tempCenter.y += _settingView.bounds.size.height;    }        // 3.重新赋值    _settingView.center = tempCenter;        [UIView commitAnimations];}#pragma mark slider值改变- (IBAction)sliderValueChange:(UISlider *)sender {    // 1.设置中间的图片    // 获得图片名称  %.f 不保留任何小数    NSString *imageName = [NSString stringWithFormat:@"%.f.png", sender.value];    _imageView.image = [UIImage imageNamed:imageName];        // 2.设置序号(第几张)    _imageNo.text = [NSString stringWithFormat:@"%.f/16", sender.value + 1];        // 3.设置描述    int no = (int)(sender.value + 0.5);    _imageDesc.text = _allDescs[no];}@end

0 0
原创粉丝点击