UILabel 標簽
@property(nullable, nonatomic,copy) NSString *text;內(nèi)容
@property(null_resettable, nonatomic,strong) UIFont *font; 字體
@property(null_resettable, nonatomic,strong) UIColor *textColor; 顏色
@property(nonatomic) NSInteger numberOfLines; 行數(shù)(0為允許任意行)
@property(nonatomic) NSTextAlignment textAlignment;對齊方式
UITextFiled 文本輸入框
@property(nullable, nonatomic,copy) NSString *text; 內(nèi)容
@property(nullable, nonatomic,strong) UIColor *textColor;內(nèi)
容顏色
@property(nullable, nonatomic,strong) UIFont *font; 字體
@property(nonatomic) NSTextAlignment textAlignment; 對齊方式
@property(nullable, nonatomic,copy) NSString *placeholder;提示信息
@property(nonatomic) UITextBorderStyle borderStyle;邊框樣式
UIButton 按鈕
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; 設置指定狀態(tài)的標題
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;設置指定狀態(tài)的標題顏色
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;設置指定狀態(tài)顯示的圖片
UIControl 控件
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;添加監(jiān)聽事件
參數(shù)1:事件觸發(fā)時執(zhí)行對象
參數(shù)2:事件觸發(fā)時執(zhí)行指定對象中的指定方法
參數(shù)3:監(jiān)聽的事件類型
UIImageView 圖片視圖
- (instancetype)initWithImage:(nullable UIImage *)image;根據(jù)圖片創(chuàng)建視圖,視圖的大小跟圖片一樣大
@property (nullable, nonatomic, strong) UIImage *image; 圖片
UIImage 圖片
+ (nullable UIImage *)imageNamed:(NSString *)name;根據(jù)名稱生成圖片
+ (nullable UIImage *)imageWithData:(NSData *)data;根據(jù)二進制生成圖片
案例1(加法計算器)
案例2(小飛機)
//定義方向枚舉
typedef NS_ENUM(NSInteger, DirType) {
DIR_UP = 101,
DIR_LEFT = 102,
DIR_DOWN = 103,
DIR_RIGHT = 104
};
@interface ViewController ()
//小飛機按鈕
@property (nonatomic,weak) UIButton *planeButton;
@end
//設置界面
-(void)setUpUI{
//設置背景視圖
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:iv];
//設置飛機
UIButton *planeButton = [[UIButton alloc] init];
[planeButton setImage:[UIImage imageNamed:@"hero1"] forState:UIControlStateNormal];
[planeButton setImage:[UIImage imageNamed:@"hero2"] forState:UIControlStateHighlighted];
[self.view addSubview:planeButton];
[planeButton sizeToFit];
planeButton.center = self.view.center;
planeButton.center = CGPointMake(self.view.center.x, self.view.center.y - 200);
//記錄變量
self.planeButton = planeButton;
//設置方向按鈕
CGFloat offset = 30;
//方向按鈕根基 偏移值CGPoint normal圖片 helight圖片 tag生成
CGPoint center = CGPointMake(self.view.center.x, self.view.center.y + 180);
[self creatButtonWith:@"top_normal" :@"top_highlighted" :CGPointMake(0, -offset) :DIR_UP :center];
[self creatButtonWith:@"left_normal" :@"left_highlighted" :CGPointMake(-offset, 0) :DIR_LEFT :center];
[self creatButtonWith:@"bottom_normal" :@"bottom_highlighted" :CGPointMake(0, offset) :DIR_DOWN :center];
[self creatButtonWith:@"right_normal" :@"right_highlighted" :CGPointMake(offset, 0) :DIR_RIGHT :center];
}
//創(chuàng)建按鈕并添加到視圖
-(void)creatButtonWith:(NSString *)normalName :(NSString *)helightName :(CGPoint)offset :(NSInteger)tag :(CGPoint)center{
UIButton *btn = [[UIButton alloc] init];
[self.view addSubview:btn];
[btn setImage:[UIImage imageNamed:normalName] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:helightName] forState:UIControlStateHighlighted];
btn.tag = tag;
btn.frame = CGRectMake(0, 0, 40, 40);
btn.center = CGPointMake(center.x + offset.x, center.y + offset.y);
[btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
}
//點擊按鈕
-(void)clickButton:(UIButton *)sender {
//獲取初始位置
CGRect frame = self.planeButton.frame;
//每次點擊移動的距離
CGFloat offset = 20;
//修改位置
switch (sender.tag) {
case DIR_UP:
frame.origin.y -= offset;
break;
case DIR_LEFT:
frame.origin.x -= offset;
break;
case DIR_DOWN:
frame.origin.y += offset;
break;
case DIR_RIGHT:
frame.origin.x += offset;
break;
default:
break;
}
//賦值
self.planeButton.frame = frame;
}
@end