iPhone 自定义键盘按键

首先注册消息通知UIKeyboardWillShowNotification :

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 

                                               object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self                                                          
selector:@selector(keyboardWillHide:)                                                                  
name:UIKeyboardWillHideNotification
object:nil];

- (IBAction)keyboardWillHide:(NSNotification *)note
{
    // 做自己要做的事情
}


在消息回叫函数中处理--添加按键
- (void)keyboardWillShow:(NSNotification *)note { 
   // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];

//典型的目标/动作模式
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];


    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES){
            [keyboard addSubview:doneButton];    //控件 也是view 又可以分成多个控件单元 入tableView cell
            break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
苹果提供了UIGestureRecognizer类,可以用来实现自定义手势。下面是一个简单的例子,实现在屏幕上绘制一个“V”形手势来触发事件: 1. 首先在视图控制器中创建一个手势识别器对象: ``` @property (nonatomic, strong) UIGestureRecognizer *gestureRecognizer; ``` 2. 在viewDidLoad方法中,创建手势识别器并将其添加到视图上: ``` self.gestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [self.view addGestureRecognizer:self.gestureRecognizer]; ``` 3. 实现手势处理方法,当手势被识别时,执行自定义的事件: ``` - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { NSArray *points = [gestureRecognizer valueForKey:@"touches"]; CGPoint startPoint = [[points firstObject] locationInView:self.view]; CGPoint endPoint = [[points lastObject] locationInView:self.view]; // 计算手势的方向 CGFloat dx = endPoint.x - startPoint.x; CGFloat dy = endPoint.y - startPoint.y; if (dx > 0 && dy < 0 && fabs(dx) > fabs(dy)) { // 手势为向右上方,执行自定义事件 [self doSomething]; } } } ``` 在上面的代码中,我们使用了touches属性获取手势的起点和终点,然后计算手势的方向,最后根据手势的方向执行自定义事件。 注意:在创建手势识别器时,需要指定手势的类型,例如: ``` UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRecognizer]; ``` 上面的代码创建了一个向右滑动的手势识别器,并将其添加到视图上。当手势被识别时,会执行handleSwipe:方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值