IOS開發基礎知識--片段4

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   for   

十七:返回到主線程進行操作,對UI進行更新只能在主線程進行

 /*將資料顯示到UI控制項,注意只能在主線程中更新UI,     另外performSelectorOnMainThread方法是NSObject的分類方法,每個NSObject對象都有此方法,     它調用的selector方法是當前調用控制項的方法,例如使用UIImageView調用的時候selector就是UIImageView的方法     Object:代表調用方法的參數,不過只能傳遞一個參數(如果有多個參數請使用對象進行封裝)     waitUntilDone:是否線程任務完成執行 */[self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES];

十八:UIImageView用法總結

//初始化UIImageView  *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];//需要設定圖片 UIImage(第一種跟第二種會Cache到記憶體,消耗記憶體,但以後訪問有優勢,當只載入一次推薦用第三種)第一種:[imageView setImage:[UIImage imageNamed:@"1.jpeg"]];//第二種:NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpeg"];UIImage *images=[UIImage imageWithContentsOfFile:filePath];//[imageView setImage:images]; //第三種:NSData *data=[NSData dataWithContentsOfFile:filePath];UIImage *image2=[UIImage imageWithData:data];[imageView setImage:image2];

十九:通過tag獲得子控制項

首先要把這個子控制項的屬性增加一個tag,    UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];    button.tag=100;    button.frame=CGRectMake(130, 310, 60, 50);    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];    [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];    [self.view addSubview:button];然後再可以通過viewWithTag去獲得,self.view是本視圖,是Button的父視圖    UIButton * button = (UIButton*)[self.view viewWithTag:100];

二十:動態修改按鍵的背景圖

帶一個參數,play:,就可以通過這個方法進行操作    //播放按鈕    UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];    button.tag=100;    button.frame=CGRectMake(130, 310, 60, 50);    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];    [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];    [self.view addSubview:button];//播放-(void)play:(UIButton*)button{    if(_audioPlayer.playing)    {        [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];        [_audioPlayer pause];    }    else    {        [button setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];        [_audioPlayer play];    }    }

二十一:判斷是不是類比機

- (void)viewDidLoad{    [super viewDidLoad];        self.title=@"判斷是否是類比機";    [self isSimulator];}-(void)isSimulator{    if (TARGET_IPHONE_SIMULATOR) {        NSLog(@"是類比機");    }else{        NSLog(@"不是類比機");    }}

二十二:16進位色彩轉化成UIColor

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];    view.backgroundColor=[self colorWithHexString:@"e26562"];    [self.view addSubview:view];    }//16進位顏色#e26562與UIColor互轉,設定View背景顏色- (UIColor *) colorWithHexString: (NSString *)color{    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];        // String should be 6 or 8 characters    if ([cString length] < 6) {        return [UIColor clearColor];    }        // strip 0X if it appears    if ([cString hasPrefix:@"0X"])        cString = [cString substringFromIndex:2];    if ([cString hasPrefix:@"#"])        cString = [cString substringFromIndex:1];    if ([cString length] != 6)        return [UIColor clearColor];        // Separate into r, g, b substrings    NSRange range;    range.location = 0;    range.length = 2;        //r    NSString *rString = [cString substringWithRange:range];        //g    range.location = 2;    NSString *gString = [cString substringWithRange:range];        //b    range.location = 4;    NSString *bString = [cString substringWithRange:range];        // Scan values    unsigned int r, g, b;    [[NSScanner scannerWithString:rString] scanHexInt:&r];    [[NSScanner scannerWithString:gString] scanHexInt:&g];    [[NSScanner scannerWithString:bString] scanHexInt:&b];        return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];}

 

IOS開發基礎知識--片段4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.