從這裡開始是UI篇 知識點: 1.常用IOS基本控制項 2.UITouch 常用基本控制項 1.UISegmentedControl:分段控制器 1)創建方式 - (id)initWithItems:(NSArray *)items; items數組中可以有NSString或者是UIImage對象 UI ...
從這裡開始是UI篇
知識點:
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];