UI小组件学习
UIActivityIndicatorView
/* UIActivityIndicatorViewStyleWhiteLarge, UIActivityIndicatorViewStyleWhite, UIActivityIndicatorViewStyleGray, */ UIActivityIndicatorView *viView=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; viView.center=CGPointMake(160, 300); viView.color=[UIColor blueColor]; viView.hidesWhenStopped=NO;//动画停止时是否隐藏 [viView startAnimating]; // [viView stopAnimating];//停止动画 [self.view addSubview:viView];
UIAlertView 与 UIActionSheet
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"通知" message:@"明天礼拜五了,大家晚上多多努力,争取在放假前把项目上架。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil,nil]; UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"你确定删除么?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
下面是各自的代理:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"点击了第%d个按键",buttonIndex); switch (buttonIndex) { case 0: break; default: break; } } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"点击了第%d个按键",buttonIndex); switch (buttonIndex) { case 0: break; default: break; } }
UISilder
- (void)viewDidLoad { [super viewDidLoad]; //实例化 UISlider *slide=[[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 60)]; //设置左侧右侧轨道的图片,ios7无效 [slide setMinimumTrackImage:[UIImage imageNamed:@"max"] forState:UIControlStateNormal]; [slide setMaximumTrackImage:[UIImage imageNamed:@"min"] forState:UIControlStateNormal]; //设置最大最小值 slide.minimumValue=0; slide.maximumValue=1; //设置当前值 [slide setValue:0.7 animated:YES]; //添加响应函数 [slide addTarget:self action:@selector(onSliderChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slide]; [self.progressView setProgress:0.7 animated:YES]; } -(void)onSliderChange:(id)sender{ NSLog(@"slider:%@",sender); UISlider *slider=sender; self.myView.alpha=slider.value; self.progressLabel.text=[NSString stringWithFormat:@"%.0f%%",slider.value*100]; self.progressLabel.center=CGPointMake(20+280*slider.value, 100); }
UISwitch
UISwitch *mySwitch=[[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; [mySwitch addTarget:self action:@selector(onSwitchChange:) forControlEvents:UIControlEventValueChanged]; mySwitch.onTintColor=[UIColor redColor]; mySwitch.on=NO; [mySwitch setOn:YES animated:YES]; [self.view addSubview:mySwitch];
UITouch
#pragma mark-- #pragma mark touch事件 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //任意取出一个touch对象 UITouch *touch = touches.anyObject; beginPoint = [touch locationInView:self.view]; if (beginPoint.x<10) { canMove=YES; }else{ canMove=NO; } NSLog(@"触摸开始 %f,%f",beginPoint.x,beginPoint.y); } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = touches.anyObject; CGPoint movePoint = [touch locationInView:self.view]; NSLog(@"触摸滑动 %f",movePoint.x-beginPoint.x); if (canMove) { redView.frame=CGRectMake(-320+movePoint.x-beginPoint.x, 0, 320, 480); } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸结束"); UITouch *touch = touches.anyObject; CGPoint endPoint = [touch locationInView:self.view]; if ((endPoint.x-beginPoint.x)>200) { [UIView animateWithDuration:0.25 animations:^{ redView.frame=CGRectMake(0, 0, 320, 480); }]; }else{ [UIView animateWithDuration:0.25 animations:^{ redView.frame=CGRectMake(-320, 0, 320, 480); }]; } } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸取消"); }