IOS--UIButton的使用方法详细

   // UIButton的常用方法

    UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom]; // 初始化时设置Button样式

    // 风格有如下

//    typedef enum {

//        UIButtonTypeCustom = 0,           // 自定义,无风格

//        UIButtonTypeRoundedRect,        // 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片

//        UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁

//        UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁

//        UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮

//        UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁

//    } UIButtonType;

    

    

    // 最常用

    oneButton.frame = CGRectMake(10, 10, 300, 30); // 设置oneButton的位置和大小

    oneButton.backgroundColor = [UIColor blueColor]; // 设置oneButton背景色

    oneButton.alpha = 0.8; // 设置oneButton的透明度范围在0.0-1.0之间

    [oneButton setTitle:@"我是一个UIButton" forState:UIControlStateNormal]; // 设置在什么状态下显示什么文字

    [oneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 设置什么状态下字体什么颜色

    [oneButton setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forState:UIControlStateNormal]; // 设置什么状态下显示的背景图像

    // 上面几个方法都提到 共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

//    enum {

//        UIControlStateNormal       = 0,//常态

//        UIControlStateHighlighted  = 1 << 0,    //  高亮

//        UIControlStateDisabled     = 1 << 1,    //禁用

//        UIControlStateSelected     = 1 << 2,    // 选中

//        UIControlStateApplication  = 0x00FF0000,// 当应用程序标志使用时

//        UIControlStateReserved     = 0xFF000000 // 为内部框架预留的

//    };

    oneButton.tag = 10001; // 设置标签,用于便于点击的是哪个按钮,常用在委托方法中

    [oneButton.titleLabel setFont:[UIFont systemFontOfSize:25.0f]]; // 设置文字的大小

    

    oneButton.adjustsImageWhenDisabled = NO; // 对按钮进行微调。当按钮禁用时,图像会被画的颜色深一些 默认是YES,禁止设置为NO

    oneButton.adjustsImageWhenHighlighted = NO; // 对按钮进行微调。当按钮高亮是,图像会被画得颜色深一些 默认是YES,禁止设置为NO

    

    oneButton.showsTouchWhenHighlighted = YES; // 设置点击的时候是否有光照得效果

    

    // 给按钮添加响应事件

    [oneButton addTarget:self action:@selector(oneButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];//当按钮被按下时会执行oneButtonTouchUpinside:方法。这个方法是自定义的,需要自己写出。参数是(UIButton *)类型

    

    // 添加到view
    [self.view addSubview:oneButton];

 

  • 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、付费专栏及课程。

余额充值