iOS開發-UI (一)常用控制項,ios開發-ui控制項

來源:互聯網
上載者:User

iOS開發-UI (一)常用控制項,ios開發-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];

 

    

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.