用UIControl封装Button

  1. 云栖社区>
  2. 博客列表>
  3. 正文

用UIControl封装Button

游贤明 2016-04-14 17:17:42 浏览577 评论0

摘要: 用UIControl封装Button   效果   说明 UIControl在处理超出触摸范围的触摸事件时有bug   源码 基础类 // // BaseControl.h // BaseControl // // Created by YouXianMing on 15/8/26.

用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

细节


用云栖社区APP,舒服~

【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口  详情请点击

网友评论

游贤明
文章763篇 | 关注46
关注
移动测试(Mobile Testing)是为广大企业客户和移动开发者提供真机测试服务的云平台... 查看详情
阿里云移动APP解决方案,助力开发者轻松应对移动app中随时可能出现的用户数量的爆发式增长、... 查看详情
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效... 查看详情
阿里云总监课正式启航

阿里云总监课正式启航