设为首页 加入收藏

TOP

用动画切换按钮的状态(一)
2017-10-13 10:28:45 】 浏览:6899
Tags:动画 切换 按钮 状态

用动画切换按钮的状态

 

效果

 

源码

https://github.com/YouXianMing/UI-Component-Collection

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

#import <UIKit/UIKit.h>
@class BaseControl;

@protocol BaseControlDelegate <NSObject>

@optional

/**
 *  点击事件触发
 *
 *  @param control BaseControl对象
 */
- (void)baseControlTouchEvent:(BaseControl *)control;

@end

@interface BaseControl : UIView

/**
 *  代理方法
 */
@property (nonatomic, weak) id <BaseControlDelegate>  delegate;

#pragma mark - 以下方法需要子类重载

/**
 *  触发了点击事件
 */
- (void)touchEvent;

/**
 *  拖拽到rect外面触发的事件
 */
- (void)touchDragExit;

/**
 *  点击事件开始
 */
- (void)touchBegin;

@end
//
//  BaseControl.m
//  BaseButton
//
//  Created by YouXianMing on 15/8/27.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"

@interface BaseControl ()

@property (nonatomic, strong) UIButton *button;

@end

@implementation BaseControl

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self baseControlSetup];
    }
    
    return self;
}

- (void)baseControlSetup {
    
    _button = [[UIButton alloc] initWithFrame:self.bounds];
    [self addSubview:_button];
    
    // 开始点击
    [_button addTarget:self action:@selector(touchBegin) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
    
    // 拖拽到rect外面
    [_button addTarget:self action:@selector(touchDragExit) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];
    
    // 触发事件
    [_button addTarget:self action:@selector(touchEvent) forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchEvent {
    
    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

- (void)touchDragExit {
    
    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

- (void)touchBegin {
    
    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

@end
//
//  CustomButton.h
//  CustomButton
//
//  Created by YouXianMing on 16/5/21.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"

typedef NS_OPTIONS(NSUInteger, BaseControlState) {
    
    BaseControlStateNormal = 1000,
    BaseControlStateHighlighted,
    BaseControlStateDisabled,
};

@interface CustomButton : BaseControl

/**
 *  目标
 */
@property (nonatomic, weak) id target;

/**
 *  按钮事件
 */
@property (nonatomic) SEL      buttonEvent;

/**
 *  普通背景色
 */
@property (nonatomic, strong) UIColor  *normalBackgroundColor;

/**
 *  高亮状态背景色
 */
@property (nonatomic, strong) UIColor  *highlightBackgroundColor;
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【原】iOS学习之KVC原理 下一篇【代码笔记】iOS-设置textView或..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目