iOS开发两个距离较近的按钮同时触发事件的解决方法
生活随笔
收集整理的這篇文章主要介紹了
iOS开发两个距离较近的按钮同时触发事件的解决方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最繁瑣的一種方法是(給按鈕加判斷):
// Created by Mini on 16/3/11. // Copyright ? 2016年 IZHUO.NET. All rights reserved. //#import "ViewController.h"@interface ViewController ()@property (nonatomic,strong,readonly) UIButton *firstButton; @property (nonatomic,strong,readonly) UIButton *secondButton;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//為第一個button添加事件_firstButton = [UIButton buttonWithType:UIButtonTypeCustom];[_firstButton addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];[_firstButton addTarget:self action:@selector(recordButtonAction:) forControlEvents:UIControlEventTouchUpInside];[_firstButton addTarget:self action:@selector(recoveryButtonAction:) forControlEvents:UIControlEventTouchUpInside];//為第二個button添加事件_secondButton = [UIButton buttonWithType:UIButtonTypeCustom];[_secondButton addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];[_secondButton addTarget:self action:@selector(recordButtonAction:) forControlEvents:UIControlEventTouchUpInside];[_secondButton addTarget:self action:@selector(recoveryButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark -- 事件響應 //點擊按鈕到離開屏幕響應事件時恢復按鈕的狀態并觸發事件 - (void)actionButton:(UIButton *)sender {_firstButton.enabled = YES;_secondButton.enabled = YES;if (sender == _firstButton) {[self firstButtonPressed];}else if (sender == _secondButton) {[self secondButtonPressed];} }// 當按鈕點下時把另一按鈕設置不可點 - (void)recordButtonAction:(UIButton *)sender {if (sender == _firstButton) {_secondButton.enabled = NO;}else if (sender == _secondButton) {_secondButton.enabled = NO;} } //當點下按鈕從按鈕區域外離開時恢復button狀態 - (void)recoveryButtonAction:(UIButton *)sender {_firstButton.enabled = YES;_secondButton.enabled = YES; }- (void)firstButtonPressed {//第一個按鈕事件處理 } - (void)secondButtonPressed {//第二個按鈕事件處理 }最簡單一種方法是:
[_firstButton setExclusiveTouch:YES];
[_secondButton setExclusiveTouch:YES];
這個是UIView的成員方法, 只要是繼承自UIView的控件都可以用。
總結
以上是生活随笔為你收集整理的iOS开发两个距离较近的按钮同时触发事件的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone之横竖屏与自动旋转
- 下一篇: iOS开发之本地通知UILocalNot