ios 添加监听

创建一个测试的UIButton

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic, strong)UIButton *button;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(30, 50, 50, 30)];
    [self.button setTitle:@"测试" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button.layer.borderWidth = 1.0f;
    [self.view addSubview:self.button];
    
    //注册监听button的enabled状态
    [self.button addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:@"test_button"];

//监听self.panel.button的enable属性,addObserver 添加的是拥有者self NSKeyValueObservingOptionNew 是新值
[self.panel.button addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:@"test_button"];
 //  3秒钟后改变当前button的enabled状态
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.button.enabled = YES;
    });
}

添加监听观察者

/**
 *  监听按钮状态改变的方法
 *
 *  @param keyPath 按钮改变的属性
 *  @param object  按钮
 *  @param change  改变后的数据
 *  @param context 注册监听时context传递过来的值
 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    UIButton *button = (UIButton *)object;
    if (self.button == button && [@"enabled" isEqualToString:keyPath]) {
        NSLog(@"self.button的enabled属性改变了%@",[change objectForKey:@"new"]);
    }
}

log输出

2015-06-30 11:48:32.001 监听控件某个属性的改变[34212:570638] self.button的enabled属性改变了1

如果你不是在wb145230博客园看到本文,请点击查看原文.

### 实现 iOS 设备上软键盘事件监听 在 Vue 项目中处理 iOS 设备上的软键盘事件,可以利用原生的通知机制 `KeyboardWillShowNotification` 和 `KeyboardWillHideNotification` 来捕获键盘的状态变化并相应调整页面布局。 #### 使用 JavaScript 监听键盘事件 对于 iOS 平台,在应用程序初始化阶段设置对特定通知的监听器: ```javascript mounted() { if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) { // 判断是否为iOS设备 window.addEventListener('focusin', this.handleFocusIn); window.addEventListener('focusout', this.handleFocusOut); const keyboardShowListener = () => console.log('keyboard will show'); const keyboardHideListener = () => console.log('keyboard will hide'); // 添加键盘显示/隐藏监听 document.addEventListener('native.keyboardshow', keyboardShowListener, false); document.addEventListener('native.keyboardhide', keyboardHideListener, false); // 移除监听函数防止内存泄漏 this.$once('hook:beforeDestroy', () => { document.removeEventListener('native.keyboardshow', keyboardShowListener, false); document.removeEventListener('native.keyboardhide', keyboardHideListener, false); window.removeEventListener('focusin', this.handleFocusIn); window.removeEventListener('focusout', this.handleFocusOut); }); } }, methods: { handleFocusIn(event) { // 处理聚焦逻辑,比如滚动到输入框可见区域 console.log(`Element ${event.target.tagName} got focus`); }, handleFocusOut(event) { // 输入框失去焦点后的操作 console.log(`Element ${event.target.tagName} lost focus`); } } ``` 需要注意的是上述代码中的 `'native.keyboardshow'` 和 `'native.keyboardhide'` 是假设性的自定义事件名称;实际开发环境中可能需要依赖第三方库或插件来适配这些行为[^1]。 另外一种方法是在不借助额外插件的情况下,基于输入域获得和失去焦点来进行简单判断。这种方法适用于只需要知道何时打开关闭而不需要精确高度的情形下[^2]。 为了更精准地控制视图随键盘弹出自动调整位置的行为,还可以考虑引入专门针对移动端优化过的 UI 组件框架或者工具包,它们通常内置了更好的支持与兼容性处理方案[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值