您的位置:首页 > 移动开发 > IOS开发

IOS开发基础知识--碎片4

2014-12-08 15:25 465 查看
十七:返回到主线程进行操作,对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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: