用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

细节


目录
相关文章
|
3月前
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
578 1
|
5月前
|
iOS开发 UED 开发者
UIControl 功能和用法
UIControl 功能和用法
element-ui框架的el-dialog弹出框被遮罩层挡住了/el-drawer....会生成v-model元素的组件被遮罩层挡住
element-ui框架的el-dialog弹出框被遮罩层挡住了/el-drawer....会生成v-model元素的组件被遮罩层挡住
428 1
dialog快速实现,无需继承Dialog类,实现播放视频!
dialog快速实现,无需继承Dialog类,实现播放视频!
dialog快速实现,无需继承Dialog类,实现播放视频!
|
数据可视化
UGUI系列-Button绑定事件的多种实现
今天分享一下UGUI Button绑定事件的几种方法,以及优点和缺点 有哪些地方不懂的小伙伴也可以联系我的QQ,我的QQ就在博客链接中隐藏着,看能不能找到咯
|
程序员 iOS开发
UIGestureRecognizer类常用属性以及方法
UIGestureRecognizer类常用属性以及方法
114 0
|
C#
【WPF】代码触发Button点击事件
原文:【WPF】代码触发Button点击事件 先定义Button按钮并绑定事件。 public void test() { Button btn = new Button(); btn.
2500 0