用UIControl封装Button

简介:

用UIControl封装Button

 

效果

 

说明

UIControl在处理超出触摸范围的触摸事件时有bug

 

源码

基础类


//
//  BaseControl.h
//  BaseControl
//
//  Created by YouXianMing on 15/8/26.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BaseControl : UIControl

/**
 *  ==== 由子类重写 ====
 *
 *  开始触发事件
 */
- (void)beginTouch;

/**
 *  ==== 由子类重写 ====
 *
 *  结束触发事件
 *
 *  @param outRange 是否超出操作范围
 */
- (void)endTouchOutOfRange:(BOOL)outRange;

@end


//
//  BaseControl.m
//  BaseControl
//
//  Created by YouXianMing on 15/8/26.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"

@interface BaseControl ()

@property (nonatomic) CGPoint  endPoint;

@end

@implementation BaseControl

#pragma mark - 
- (void)beginTouch {

}

- (void)endTouchOutOfRange:(BOOL)outRange {

}

#pragma mark - UIControl事件
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    [self beginTouch];
    
    return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    return YES;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    
    self.endPoint = [touch locationInView:self];
}

- (void)cancelTrackingWithEvent:(UIEvent *)event {
    
    [super cancelTrackingWithEvent:event];
}

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    CGPoint point = self.endPoint;
    if (point.x >= 0 && point.x <= self.bounds.size.width && point.y >= 0 && point.y <= self.bounds.size.height) {

        [self endTouchOutOfRange:NO];
        [super sendAction:action to:target forEvent:event];
        
    } else {
    
        [self endTouchOutOfRange:YES];
        [super sendAction:action to:target forEvent:event];
    }
}

@end


//
//  ColorButton.h
//  BaseControl
//
//  Created by YouXianMing on 15/8/26.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"

@interface ColorButton : BaseControl

@end


//
//  ColorButton.m
//  BaseControl
//
//  Created by YouXianMing on 15/8/26.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ColorButton.h"

@implementation ColorButton

- (void)beginTouch {

    [UIView animateWithDuration:0.4f delay:0.f usingSpringWithDamping:1.f initialSpringVelocity:0.f options:0 animations:^{
        
        self.backgroundColor = [UIColor redColor];
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)endTouchOutOfRange:(BOOL)outRange {

    [UIView animateWithDuration:0.4f delay:0.f usingSpringWithDamping:1.f initialSpringVelocity:0.f options:0 animations:^{
        
        self.backgroundColor = [UIColor whiteColor];
        
    } completion:^(BOOL finished) {
        
    }];
}

@end


//
//  ViewController.m
//  BaseControl
//
//  Created by YouXianMing on 15/8/26.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "ColorButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    ColorButton *control      = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    control.center            = self.view.center;
    control.layer.borderWidth = 1.f;
    [self.view addSubview:control];
    
    [control addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonEvent:(id)sender {

}

@end

细节


目录
相关文章
|
2月前
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Push Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Push Button的使用及说明
136 0
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Push Button的使用及说明
|
2月前
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
384 0
|
4月前
|
iOS开发 UED 开发者
UIControl 功能和用法
UIControl 功能和用法
Qt多个button信号槽操作
Qt多个button信号槽操作
72 0
|
JavaScript
elementUI封装 el-dialog
elementUI封装 el-dialog
elementUI封装 el-dialog
dialog快速实现,无需继承Dialog类,实现播放视频!
dialog快速实现,无需继承Dialog类,实现播放视频!
dialog快速实现,无需继承Dialog类,实现播放视频!
|
数据可视化
UGUI系列-Button绑定事件的多种实现
今天分享一下UGUI Button绑定事件的几种方法,以及优点和缺点 有哪些地方不懂的小伙伴也可以联系我的QQ,我的QQ就在博客链接中隐藏着,看能不能找到咯
|
C#
【WPF】代码触发Button点击事件
原文:【WPF】代码触发Button点击事件 先定义Button按钮并绑定事件。 public void test() { Button btn = new Button(); btn.
2492 0
|
内存技术 数据格式 XML