当前位置:首页 > 风云人物 > UIControl学习笔记

UIControl学习笔记

2024-02-08 22:18:54 来源:金风故事网
UIControl 简介

UIControl派生自UIView类,是作为响应用户交互而传递特定操作或意图的可视控件的基类。UISwitch开关、UIButton按钮、UISegmentedControl分段控件、UISlider滑块、UITextField文本字段控件、UIPageControl分页控件均派生自UIControl。

UIControl 常用属性
@property(nonatomic,getter=isEnabled) BOOL enabled;

属性描述 :默认为“YES”,如果为“NO”则忽略触摸事件,子类可能会有不同的默认值。

@property(nonatomic,getter=isSelected) BOOL selected; 

属性描述 :默认为“NO”,当用户选中控件时,UIControl类会将其selected属性设置为YES。子类有时使用这个属性来让控件选择自身,或者来表现不同的行为方式。

@property(nonatomic,getter=isHighlighted) BOOL highlighted; 

属性描述 :指示控件是否绘制高亮显示的布尔值,默认为“NO”。当此属性的值设置为YES时,控件将高亮显示,否则控件将不会绘制高亮显示。控件自动设置和清除此状态以响应适当的触摸事件,可以根据需要更改此属性的值,以编程方式应用或删除高亮显示。故事板文件中可以设置控件的初始选定状态。

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;

属性描述 :控件边界内内容的垂直对齐方式,默认为中心。

UIControlContentVerticalAlignment枚举值如下:typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {    UIControlContentVerticalAlignmentCenter        = 0,//控件内容垂直对齐中心    UIControlContentVerticalAlignmentTop           = 1,//控件内容垂直对齐顶部    UIControlContentVerticalAlignmentBottom     = 2,//控件内容垂直对齐底部    UIControlContentVerticalAlignmentFill          = 3,//将内容垂直对齐以填充内容矩形,图像可能会被拉长。};
@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, readonly) UIControlContentHorizontalAlignment effectiveContentHorizontalAlignment;

属性描述 :只读属性,在设置UIControlContentHorizontalAlignmentLeading或UIControlContentHorizontalAlignmentTrailing时返回UIControlContentHorizontalAlignmentLeft或UIControlContentHorizontalAlignmentRight

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   //所有事件};

//  ShadowViewController.h#import <UIKit/UIKit.h>@interface ShadowViewController : UIViewControlle@end
//  ShadowViewController.m#import "ShadowViewController.h"#import "ShadowView.h"@interface ShadowViewController ()@end@implementation ShadowViewController- (void)viewDidLoad {    [super viewDidLoad];    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.layer.borderWidth= 1.5;    [button setTitle:@"完成" forState:UIControlStateNormal];    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [button addTarget:self action:@selector(buttonTestClicked:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    button.translatesAutoresizingMaskIntoConstraints = NO;    [NSLayoutConstraint activateConstraints:@[        [button.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor constant:-45 / 2],        [button.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],        [button.widthAnchor constraintEqualToConstant:75],        [button.heightAnchor constraintEqualToConstant:45],    ]];}- (void)buttonTestClicked:(UIButton *)sender {    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)[UIColor redColor].CGColor, (id)[UIColor orangeColor].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()@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.hidden = YES;        [self drawView];    }    return self;}- (void)drawView{    ///视图控制层    self.backControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];    self.backControl.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];    [self.backControl addTarget:self action:@selector(backControlClick) forControlEvents:UIControlEventTouchUpInside];    self.backControl.alpha = 0;    self.backgroundColor = [UIColor clearColor];    ///底部文字描述视图    self.characterDescriptionView = [[UIScrollView alloc]initWithFrame:CGRectZero];    self.characterDescriptionView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.3];    self.characterDescriptionView.scrollEnabled = YES;    self.characterDescriptionView.layer.cornerRadius = 10.0;    self.characterDescriptionView.layer.masksToBounds = YES;    [self addSubview:self.characterDescriptionView];    self.characterDescriptionView.translatesAutoresizingMaskIntoConstraints = NO;    [NSLayoutConstraint activateConstraints:@[        [self.characterDescriptionView.topAnchor constraintEqualToAnchor:self.topAnchor constant:10],        [self.characterDescriptionView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:15],        [self.characterDescriptionView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-15],        [self.characterDescriptionView.heightAnchor constraintEqualToConstant: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.translatesAutoresizingMaskIntoConstraints = NO;    [NSLayoutConstraint activateConstraints:@[        [self.characterDescriptionLabel.topAnchor constraintEqualToAnchor:self.characterDescriptionView.topAnchor constant:10],        [self.characterDescriptionLabel.bottomAnchor constraintEqualToAnchor:self.characterDescriptionView.bottomAnchor constant:-10],        [self.characterDescriptionLabel.widthAnchor constraintEqualToConstant:280],    ]];}///显示视图- (void)showInView:(UIView *)view{    if (self.isHidden) {        self.hidden = NO;        if (self.backControl.superview == nil) {            [view addSubview:self.backControl];        }        [UIView animateWithDuration:0.2f animations:^{            self.backControl.alpha = 1;        }];        [view addSubview:self];        self.translatesAutoresizingMaskIntoConstraints = NO;        [NSLayoutConstraint activateConstraints:@[            [self.centerXAnchor constraintEqualToAnchor:view.centerXAnchor],            [self.centerYAnchor constraintEqualToAnchor:view.centerYAnchor],            [self.widthAnchor constraintEqualToConstant:300],            [self.heightAnchor constraintEqualToConstant:190],        ]];    }}///隐藏视图- (void)hideInView{    if (!self.isHidden) {        self.hidden = YES;        [UIView animateWithDuration:0.2f animations:^{            self.backControl.alpha=0;        }completion:^(BOOL finished) {            [self removeFromSuperview];        }];    }}///控制层点击事件- (void)backControlClick{    [self hideInView];}///设置红包玩法文本描述- (void)setSignLabel:(NSString *)signLabel{    self.characterDescriptionLabel.text = [self handleHtmlText:signLabel];    self.characterDescriptionView.contentSize = CGSizeMake(0, [self.characterDescriptionLabel sizeThatFits:CGSizeMake(280, MAXFLOAT)].height);}///处理带<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] co(温柔干净的网名有哪些:1、盈手赠佳期2、夏树繁花3、零皮冰点4、薄荷微风的夏5、与日暮同辉6、情归何处7、声音好听8、起司奶香猫9、美人如玉)ntainsString:@"<p>"]){                NSString *newStr = [strArray[i] stringByReplacingOccurrencesOfString:@"<p>" withString:@"•"];                NSMutableString *mutableString = [[NSMutableString alloc]initWithString:newStr];                [mutableString appendString:@"\r"];                [mutableStr appendString:mutableString];            }        }    }    return mutableStr;}@end

效果:

Jietu20220803-111518-HD.gif
相关内容推荐
风云人物最新文章
精华推荐
热门图文