从这里开始是UI篇
知识点:
class="Apple-tab-span"> 1.常用IOS基本控件
2.UITouch
=======================
常用基本控件
1.UISegmentedControl:分段控制器
1)创建方式
- (id)initWithItems:(NSArray *)items;
items数组中可以有NSString或者是UIImage对象
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"广州",@"深圳",@"珠海"]];
2)常用属性
设置标题/图片(注意下标范围从0~segments-1)
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment [seg setImage:[UIImage imageNamed:@"refresh_30"] forSegmentAtIndex:2];
插入标题/图片
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated - (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated
//插入
[seg insertSegmentWithTitle:@"湛江" atIndex:3 animated:YES];
移除内容 - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated - (void)removeAllSegments //移除某一个分段 [seg removeSegmentAtIndex:0 animated:YES]; //移除所有分段 [seg removeAllSegments];
- (void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
3)事件处理
UIControlEventValueChanged
//添加事件
//注意事件类型使用:UIControlEventValueChanged
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
2.UISlider:滑块
1)创建方式
2)常用属性
当前value
property(nonatomic) float value
//实例化一个UISlider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 80, 200, 100)];
//设置默认滑块的位置(默认是0 - 1范围)
slider.value = 0.5;
最小value
@property(nonatomic) float minimumValue
最大value
@property(nonatomic) float maximumValue
//修改最大最小值
slider.minimumValue = 10;
slider.maximumValue = 100;
3)定制UI
@property(nonatomic,retain) UIColor *minimumTrackTintColor @property(nonatomic,retain) UIColor *maximumTrackTintColor @property(nonatomic,retain) UIColor *thumbTintColor //添加事件 [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; //设置左右颜色 slider.minimumTrackTintColor = [UIColor redColor]; slider.maximumTrackTintColor = [UIColor yellowColor]; slider.thumbTintColor = [UIColor purpleColor];
3.UISwitch:开关控件
1)创建方式
// 实例化一个UISwitch
UISwitch *swi = [[UISwitch alloc] initWithFrame:CGRectMake(30, 80, 10, 10)];
2)常用属性
@property(nonatomic, retain) UIColor *onTintColor
@property(nonatomic, retain) UIColor *thumbTintColor
@property(nonatomic,getter=isOn) BOOL on
//设置按键颜色
swi.thumbTintColor = [UIColor orangeColor];
//打开的颜色
swi.onTintColor = [UIColor purpleColor];
//设置开关状态
swi.on = NO;
3)事件处理
[swi addTarget:self action:@selector(swiAction:) forControlEvents:UIControlEventValueChanged];
4.UIActivityIndicatorView
1)创建方式
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
UIActivityIndicatorViewStyleWhiteLarge 大白色
UIActivityIndicatorViewStyleWhite 普通大小白
UIActivityIndicatorViewStyleGray 普通大小灰
// 加载视图
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30, 100, 200, 200)];
act.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
2)常用属性
开始转动
- (void)startAnimating;
停止转动
- (void)stopAnimating;
是否正在转动
- (BOOL)isAnimating;
颜色
@property (readwrite, nonatomic, retain) UIColor *color
//设置颜色
act.color = [UIColor orangeColor];
//开启动画
[act startAnimating];
//停止动画
[act stopAnimating];
//设置停止动画依然显示
act.hidesWhenStopped = NO;
5.UIProgressView:进度条
1)创建方式
- (id)initWithProgressViewStyle:(UIProgressViewStyle)style
UIProgressView *pro = [[UIProgressView alloc] initWithFrame:CGRectMake(30, 100, 200, 30)];
2)常用属性
@property(nonatomic) float progress 当前进度
@property(nonatomic, retain) UIColor* trackTintColor
@property(nonatomic, retain) UIColor* progressTintColor
//默认的范围为0 - 1
//设置进度
pro.progress = 0.5;
//完成进度的颜色
pro.progressTintColor = [UIColor redColor];
//未完成进度的颜色
pro.trackTintColor = [UIColor greenColor];
练习:模仿进度读取状态
6.UIActionSheet
代理方法
//实例化一个UIActionSheet UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"温馨提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"吃饭" otherButtonTitles:@"逛街",@"打游戏", @"睡觉",nil]; //显示UIActionSheet [sheet showInView:self.view]; - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex //点击回调代理方法 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"buttonIndex = %ld",buttonIndex); switch (buttonIndex) { case 0: { NSLog(@"吃饭"); } break; default: break; } }
7.UIAlertView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//实例化一个UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"余额不足" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"抢银行",@"搬砖",@"找个富婆", nil];
//展示
[alert show];
}
#pragma mark- UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"buttonIndex = %ld",buttonIndex);
}
=======================
UITouch
1.如何捕捉触摸事件
触摸开始 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 触摸结束 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 触摸被取消(触摸时候程序被中断) - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //获得起始坐标 //取得触摸点对象 UITouch *touch = [touches anyObject]; //转换成坐标点 CGPoint point = [touch locationInView:self.view]; }
2.如何获取坐标信息
1)获取到触摸点
UITouch *touch=[touches anyObject]
//获得起始坐标
//取得触摸点对象
UITouch *touch = [touches anyObject];
2)转换为坐标点
[touch locationInView:self.view]
CGPoint point = [touch locationInView:self.view];