UIControl 简介
UIControl派生自UIView类,作为控件的基类,是响应用户交互而传递特定操作或意图的可视元素。UISwitch开关、UIButton按钮、UISegmentedControl分段控件、UISlider滑块、UITextField文本字段控件、UIPageControl分页控件均派生自UIControl。
UIControl 常用属性
@property(nonatomic,getter=isEnabled) BOOL enabled;
属性描述 :默认为“YES”,如果为“NO”则忽略触摸事件,子类可能会有不同的默认值
@property(nonatomic,getter=isEnabled) BOOL enabled;
@property(nonatomic,getter=isSelected) BOOL selected;
属性描述 :默认为“NO”,当用户选中控件时,UIControl类会将其selected属性设置为YES。子类有时使用这个属性来让控件选择自身,或者来表现不同的行为方式。
@property(nonatomic,getter=isSelected) BOOL selected;
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
属性描述 :指示控件是否绘制高亮的布尔值。默认为“NO”。当触控进入/退出时自动设置/清除。
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
属性描述 :控件边界内内容的垂直对齐方式,默认为中心。
UIControlContentVerticalAlignment枚举值如下:
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
UIControlContentVerticalAlignmentCenter = 0,//控件内容垂直对齐中心
UIControlContentVerticalAlignmentTop = 1,//控件内容垂直对齐顶部
UIControlContentVerticalAlignmentBottom = 2,//控件内容垂直对齐底部
UIControlContentVerticalAlignmentFill = 3,
};
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
属性描述 :控件内容水平对齐方式,默认为中心。
UIControlContentHorizontalAlignment枚举值如下:
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
UIControlContentHorizontalAlignmentCenter = 0,//控制内容水平对齐中心
UIControlContentHorizontalAlignmentLeft = 1,//控制内容水平对齐左侧
UIControlContentHorizontalAlignmentRight = 2,//控制内容水平对齐右侧
UIControlContentHorizontalAlignmentFill = 3,
UIControlContentHorizontalAlignmentLeading API_AVAILABLE(ios(11.0), tvos(11.0)) = 4,//控制内容水平对齐头部
UIControlContentHorizontalAlignmentTrailing API_AVAILABLE(ios(11.0), tvos(11.0)) = 5,控制内容水平对齐尾部
};
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
@property(nonatomic, readonly) UIControlContentHorizontalAlignment effectiveContentHorizontalAlignment;
属性描述 :只读属性,在设置UIControlContentHorizontalAlignmentLeading或UIControlContentHorizontalAlignmentTrailing时返回UIControlContentHorizontalAlignmentLeft或UIControlContentHorizontalAlignmentRight
@property(nonatomic, readonly) UIControlContentHorizontalAlignment effectiveContentHorizontalAlignment;
UIControl事件类型的枚举
UIControl类提供了一个标准的事件通知机制来进行事件注册和接收,可以在指定的控件在发生特定的事件时,通知代理类的一个方法。事件可以用逻辑OR合并在一起,因此可以再一次单独的addTarget调用中指定多个事件。例如注册一个事件:
[self.backControl addTarget:self action:@selector(backControlClick) forControlEvents:UIControlEventTouchUpInside];
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // 控件被按下去的事件
UIControlEventTouchDownRepeat = 1 << 1, // 控件被重复点击的事件,点击次数超过一次
UIControlEventTouchDragInside = 1 << 2, //在控件范围内按下并拖动的事件
UIControlEventTouchDragOutside = 1 << 3, //在控件范围内按下并在控件外面拖动的事件
UIControlEventTouchDragEnter = 1 << 4, //从控件范围外拖动到控件范围内的事件
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6, //点击控件后在控件范围内释放触发事件
UIControlEventTouchUpOutside = 1 << 7, //点击控件后在控件范围外释放触发事件
UIControlEventTouchCancel = 1 << 8, //触摸取消事件
UIControlEventValueChanged = 1 << 12, // 当控件的值发生改变时触发的事件
UIControlEventPrimaryActionTriggered API_AVAILABLE(ios(9.0)) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, //文本控件开始编辑时触发的事件
UIControlEventEditingChanged = 1 << 17, //文本控件中的内容被改变时触发的事件
UIControlEventEditingDidEnd = 1 << 18, //文本控件结束编辑时触发的事件
UIControlEventEditingDidEndOnExit = 1 << 19, //文本控件内通过按下回车(或等价行为)结束编辑时触发的事件
UIControlEventAllTouchEvents = 0x00000FFF, //所有触摸事件
UIControlEventAllEditingEvents = 0x000F0000, // 所有关于文本编辑的事件
UIControlEventApplicationReserved = 0x0F000000, // 为应用程序预留
UIControlEventSystemReserved = 0xF0000000, // 为系统内部框架预留
UIControlEventAllEvents = 0xFFFFFFFF //所有事件
};
利用UIControl点击阴影可以消失的阴影视图代码记录
// ShadowViewController.h
#import <UIKit/UIKit.h>
@interface ShadowViewController : UIViewController
@end
// ShadowViewController.m
#import "ShadowViewController.h"
#import "shadowView.h"
@interface ShadowViewController ()
@end
@implementation ShadowViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *signLabel = @"<p>连续浇水7天,可得20鲸豆可得15.00元红包;</p><p>连续浇水14天,可得40鲸豆可得15.00元红包;</p><p>连续浇水21天,可得60鲸豆可得15.00元红包;</p><p>连续浇水28天,可得80鲸豆可得15.00元红包;";
shadowView *shadow = [[shadowView alloc]initWithFrame:CGRectZero];
shadow.signLabel = signLabel;
[shadow showInView:self.view];
[self setGradualChangeColorView:shadow];
}
///设置红包玩法背景渐变色
- (void)setGradualChangeColorView:(UIView *)view{
//CAGradientLayer继承CALayer,可以设置渐变图层
CAGradientLayer *grandientLayer = [[CAGradientLayer alloc] init];
grandientLayer.frame = CGRectMake(0, 0, 300, 330);
[view.layer addSublayer:grandientLayer];
[view.layer insertSublayer:grandientLayer atIndex:0];
//设置渐变的方向 左上(0,0) 右下(1,1)
grandientLayer.startPoint = CGPointZero;
grandientLayer.endPoint = CGPointMake(0.0, 1.0);
//colors渐变的颜色数组 这个数组中只设置一个颜色是不显示的
grandientLayer.colors = @[(id)HEXCOLOR(0xfe5153).CGColor, (id)HEXCOLOR(0xfa2629).CGColor];
grandientLayer.type = kCAGradientLayerAxial;
}
@end
// shadowView.h
#import <UIKit/UIKit.h>
@interface shadowView : UIView
@property (nonatomic, copy) NSString *signLabel;//红包玩法描述
- (void)showInView:(UIView *)view;
- (void)hideInView;
@end
// shadowView.m
#import "shadowView.h"
@interface shadowView()<CAAnimationDelegate>
@property (nonatomic, strong) UIControl * backControl;//视图控制层
@property (nonatomic, strong) UIScrollView *characterDescriptionView;//底部文字描述视图
@property (nonatomic, strong) UILabel *characterDescriptionLabel;//文字描述标签
@end
@implementation shadowView
- (id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.layer.cornerRadius = 10.0f;
self.layer.masksToBounds = YES;
[self drawView];
}
return self;
}
- (void)drawView{
self.hidden = YES;
///视图控制层
self.backControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
self.backControl.backgroundColor = RGBA(0, 0, 0, 0.5);
[self.backControl addTarget:self action:@selector(backControlClick) forControlEvents:UIControlEventTouchUpInside];
self.backControl.alpha = 0;
self.backgroundColor = [UIColor clearColor];
///头部图片
UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
[headerImageView setImage:[UIImage imageNamed:@"bonus-layer-bg"]];
headerImageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:headerImageView];
[headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top);
make.left.right.equalTo(self);
make.height.mas_equalTo(150);
}];
///底部文字描述视图
self.characterDescriptionView = [[UIScrollView alloc]initWithFrame:CGRectZero];
self.characterDescriptionView.backgroundColor = [UIColor clearColor];
self.characterDescriptionView.scrollEnabled = YES;
self.characterDescriptionView.alpha = 1;
self.characterDescriptionView.layer.cornerRadius = 10.0;
self.characterDescriptionView.layer.masksToBounds = YES;
[self addSubview:self.characterDescriptionView];
[self.characterDescriptionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(headerImageView.mas_bottom);
make.left.equalTo(self.mas_left).offset(15);
make.right.equalTo(self.mas_right).offset(-15);
//距离底部留有10间距,不留间距为330-150 = 180;
make.height.mas_equalTo(170);
}];
///遮罩视图
UIView *coverView = [[UIView alloc]initWithFrame:CGRectZero];
coverView.backgroundColor = [UIColor whiteColor];
coverView.alpha = 0.2f;
coverView.layer.cornerRadius = 10.0;
coverView.layer.masksToBounds = YES;
[self.characterDescriptionView addSubview:coverView];
[coverView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(headerImageView.mas_bottom);
make.left.equalTo(self.mas_left).offset(15);
make.right.equalTo(self.mas_right).offset(-15);
//距离底部留有10间距,不留间距为330-150 = 180;
make.height.mas_equalTo(170);
}];
///文字描述标签
self.characterDescriptionLabel = [[UILabel alloc]initWithFrame:CGRectZero];
self.characterDescriptionLabel.font = [UIFont systemFontOfSize:16];
self.characterDescriptionLabel.backgroundColor = [UIColor clearColor];
self.characterDescriptionLabel.textColor = [UIColor whiteColor];
self.characterDescriptionLabel.numberOfLines = 0;
[self.characterDescriptionView addSubview:self.characterDescriptionLabel];
[self.characterDescriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.characterDescriptionView.mas_top).offset(10);
make.bottom.equalTo(self.characterDescriptionView.mas_bottom).offset(-10);
//标签的宽度等于self的宽度减去self与滚动视图的左右间距减去滚动视图与标签的左右间距
make.width.mas_equalTo(280);
}];
}
///显示视图
- (void)showInView:(UIView *)view{
if (self.isHidden) {
self.hidden = NO;
if (self.backControl.superview == nil) {
[view addSubview:self.backControl];
}
[UIView animateWithDuration:0.2 animations:^{
self.backControl.alpha = 1;
}];
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.2;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.type = kCATransitionFromTop;
[self.layer addAnimation:animation forKey:@"animation1"];
//布局视图
[view addSubview:self];
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(view);
make.size.mas_equalTo(CGSizeMake(300, 330));
}];
}
}
///隐藏视图
- (void)hideInView{
if (!self.isHidden) {
self.hidden = YES;
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.2f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFade;
[self.layer addAnimation:animation forKey:@"animtion2"];
[UIView animateWithDuration:0.2 animations:^{
self.backControl.alpha=0;
}completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
///控制层点击事件
- (void)backControlClick{
[self hideInView];
}
///处理带<p></p>的字符串
- (NSMutableString *)handleHtmlText:(NSString *)htmlText{
NSMutableString *mutableStr = [[NSMutableString alloc]init];
if([htmlText containsString:@"</p>"]){
NSArray *strArray = [htmlText componentsSeparatedByString:@"</p>"];
for(int i = 0; i < strArray.count; i++){
if([strArray[i] containsString:@"<p>"]){
NSString *newStr = [strArray[i] stringByReplacingOccurrencesOfString:@"<p>" withString:@"•"];
NSMutableString *mutableString = [[NSMutableString alloc]initWithString:newStr];
[mutableString appendString:@"\r"];
[mutableStr appendString:mutableString];
}
}
}
return mutableStr;
}
///设置红包玩法文本描述
- (void)setSignLabel:(NSString *)signLabel{
self.characterDescriptionLabel.text = [self handleHtmlText:signLabel];
self.characterDescriptionView.contentSize = CGSizeMake(0, [self.characterDescriptionLabel sizeThatFits:CGSizeMake(280, MAXFLOAT)].height);
}
@end
效果:
Jietu20191127-210202-HD.gif
网友评论