设为首页 加入收藏

TOP

用动画切换按钮的状态(二)
2017-10-13 10:28:45 】 浏览:6902
Tags:动画 切换 按钮 状态
/** * 禁用状态背景色 */ @property (nonatomic, strong) UIColor *disabledBackgroundColor; /** * 状态值 */ @property (nonatomic, readonly) BaseControlState state; /** * 按钮标题 */ @property (nonatomic, strong) NSString *title; /** * 字体 */ @property (nonatomic, strong) UIFont *font; /** * 水平位移 */ @property (nonatomic) CGFloat horizontalOffset; /** * 垂直位移 */ @property (nonatomic) CGFloat verticalOffset; /** * 对其方式 */ @property (nonatomic) NSTextAlignment textAlignment; /** * 给标题设置颜色 * * @param color 颜色 * @param state 状态 */ - (void)setTitleColor:(UIColor *)color state:(BaseControlState)state; /** * 切换到不同的状态 * * @param state 状态 * @param animated 是否执行动画 */ - (void)changeToState:(BaseControlState)state animated:(BOOL)animated; @end
//
//  CustomButton.m
//  CustomButton
//
//  Created by YouXianMing on 16/5/21.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "CustomButton.h"

@interface CustomButton ()

@property (nonatomic) BaseControlState  state;
@property (nonatomic) BOOL              enableEvent;
@property (nonatomic, strong) UILabel  *normalLabel;
@property (nonatomic, strong) UILabel  *highlightedLabel;
@property (nonatomic, strong) UILabel  *disabledLabel;
@property (nonatomic, strong) UIView   *backgroundView;

@end

@implementation CustomButton

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        // 激活
        _enableEvent = YES;
        
        // 背景view
        self.backgroundView                 = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.backgroundView];
        
        // Label
        self.normalLabel               = [[UILabel alloc] initWithFrame:self.bounds];
        self.normalLabel.textAlignment = NSTextAlignmentCenter;
        self.normalLabel.textColor     = [UIColor clearColor];
        [self addSubview:self.normalLabel];
        
        self.highlightedLabel               = [[UILabel alloc] initWithFrame:self.bounds];
        self.highlightedLabel.textAlignment = NSTextAlignmentCenter;
        self.highlightedLabel.textColor     = [UIColor clearColor];
        [self addSubview:self.highlightedLabel];
        
        self.disabledLabel               = [[UILabel alloc] initWithFrame:self.bounds];
        self.disabledLabel.textAlignment = NSTextAlignmentCenter;
        self.disabledLabel.textColor     = [UIColor clearColor];
        [self addSubview:self.disabledLabel];
        
        // backgroundView
        self.backgroundView.userInteractionEnabled   = NO;
        self.normalLabel.userInteractionEnabled      = NO;
        self.highlightedLabel.userInteractionEnabled = NO;
        self.disabledLabel.userInteractionEnabled    = NO;
    }
    
    return self;
}

- (void)setTitleColor:(UIColor *)color state:(BaseControlState)state {
    
    if (state == BaseControlStateNormal) {
        
        self.normalLabel.textColor = color;
        
    } else if (state == BaseControlStateHighlighted) {
        
        self.highlightedLabel.textColor = color;
        
    } else if (state == BaseControlStateDisabled) {
        
        self.disabledLabel.textColor = color;
    }
}

#pragma mark - 重载的方法

- (void)touchEvent {
    
    if (_enableEvent == NO) {
        
        return;
    }
    
    [self changeToState:BaseControlStateNormal animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(baseControlTouchEvent:)]) {
        
        [self.dele
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【原】iOS学习之KVC原理 下一篇【代码笔记】iOS-设置textView或..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目