IOS_UI_day2_UIButton

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个手势解锁的Demo,你可以参考一下: ``` import UIKit class GestureLockViewController: UIViewController { // MARK: - Properties var buttons = [UIButton]() var selectedButtons = [UIButton]() var lines = [CAShapeLayer]() var touchPoint: CGPoint? var isTouching = false // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white let margin: CGFloat = 40 let distance: CGFloat = 80 let buttonWidth: CGFloat = 60 let buttonHeight: CGFloat = 60 let viewWidth = view.bounds.width let viewHeight = view.bounds.height for i in 0..<9 { let row = CGFloat(i / 3) let col = CGFloat(i % 3) let x = margin + col * (buttonWidth + distance) let y = margin + row * (buttonHeight + distance) let button = UIButton(frame: CGRect(x: x, y: y, width: buttonWidth, height: buttonHeight)) button.layer.cornerRadius = buttonWidth / 2 button.layer.borderWidth = 2 button.layer.borderColor = UIColor.lightGray.cgColor button.tag = i view.addSubview(button) buttons.append(button) } } // MARK: - Gesture Methods override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first else { return } let point = touch.location(in: view) for button in buttons { if button.frame.contains(point) && !selectedButtons.contains(button) { touchPoint = button.center selectedButtons.append(button) isTouching = true break } } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { guard isTouching, let touch = touches.first else { return } let point = touch.location(in: view) touchPoint = point for button in buttons { if button.frame.contains(point) && !selectedButtons.contains(button) { button.isSelected = true selectedButtons.append(button) let line = CAShapeLayer() line.strokeColor = UIColor.gray.cgColor line.fillColor = UIColor.clear.cgColor line.lineWidth = 3 view.layer.addSublayer(line) lines.append(line) break } } drawLines() } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { isTouching = false touchPoint = nil for button in buttons { button.isSelected = false } validatePassword() clearSelectedButtons() clearLines() } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { isTouching = false touchPoint = nil for button in buttons { button.isSelected = false } clearSelectedButtons() clearLines() } // MARK: - Private Methods private func drawLines() { guard let point = touchPoint else { return } let linePath = UIBezierPath() linePath.move(to: point) for button in selectedButtons { linePath.addLine(to: button.center) } if let lastButton = selectedButtons.last, isTouching { linePath.addLine(to: lastButton.convert(lastButton.center, to: view)) } lines.last?.path = linePath.cgPath } private func clearSelectedButtons() { for button in selectedButtons { button.isSelected = false } selectedButtons.removeAll() } private func clearLines() { for line in lines { line.removeFromSuperlayer() } lines.removeAll() } private func validatePassword() { let password = selectedButtons.map { "\($0.tag)" }.joined() print("Gesture password: \(password)") } } ``` 这个Demo实现了一个3x3的手势解锁界面,使用了`UIButton`和`CAShapeLayer`来实现。当用户滑动手指时,会根据手指位置,自动连接之前选的按钮,形成一条线。当用户抬起手指时,会根据选的按钮的顺序,输出一个密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值