_passwordField = [[UITextField alloc] init]; _passwordField.backgroundColor =[UIColor whiteColor]; _passwordField.textColor = [UIColor grayColor]; _passwordField.placeholder = @"请输入您的密码"; _passwordField.secureTextEntry = YES; _passwordField.font = [UIFont systemFontOfSize:14]; _passwordField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pwdLeftView.png"]]; _passwordField.leftViewMode = UITextFieldViewModeAlways; [self.view addSubview:_passwordField];
@endapp
// 往导航栏的左边添加一个button 第一种方式 UIBarButtonItem * leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)]; self.navigationItem.leftBarButtonItem = leftBarButtonItem; [leftBarButtonItem release];
// 往导航栏右边添加button 第二种方式 UIBarButtonItem * rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil]; self.navigationItem.rightBarButtonItem = rightBarButtonItem; [rightBarButtonItem release];字体
UIButton * backButton = [Help createButtonWithTitle:@"返回" AndFrame:CGRectMake(0, 0, 40, 40) AndTitleNormalColor:[UIColor redColor] AndTitleHighLightedColor:[UIColor blueColor] AndButtonBGC:[UIColor cyanColor] AndNormalImage:nil AndHighlightedImage:nil AndTarget:nil AndSEL:nil]; UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton]; self.navigationItem.rightBarButtonItem = backBarButtonItem;
@end动画
// 一、隐藏状态栏 [application setStatusBarHidden:NO]; // 如何隐藏状态栏 //一、须要在info.plist中添加一个字段,View controller-based status bar appearance,而且设置为NO // 二、在本方法中添加一句代码,代码为[application setStatusBarHidden:YES]; // 状态栏高度为20,导航栏高度为44 // 如何设置导航栏的背景颜色 // 设置导航栏背景颜色时,状态栏颜色也会跟着改变 [[UINavigationBar appearance]setBarTintColor:[UIColor blackColor]]; // 如何设置状态栏上面的字体颜色 [application setStatusBarStyle:UIStatusBarStyleLightContent]; #if 0 UIStatusBarStyleDefault = 0, // Dark content, for use on light backgrounds UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgroundsui
UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1, UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
#endifurl
// 改变状态栏背景颜色的步骤? /* 一、View controller-based status bar appearance,而且设置为NO 二、本方法中添加一行代码[application setStatusBarStyle:UIStatusBarStyleLightContent]; */
@endcode
动画的实现 // button点击事件触发方法orm
(void)buttonClick:(UIButton *)sender{ // 如何经过tag获取子view UIImageView * imageView = (UIImageView *)[self.window viewWithTag:500]; #if 0 #pragma mark --注释代码 // 一、开始动画 [UIView beginAnimations:nil context:NULL]; // 二、设置动画持续时间 [UIView setAnimationDuration:1.0]; // sender.tag 默认为0 if ((sender.tag = !sender.tag)) { // 奇数次点击button进入到这个if语句 // 改变imageView的y坐标 imageView.center = CGPointMake(imageView.center.x, imageView.center.y+200);对象
}else{ // 偶数次点击进入else imageView.center = CGPointMake(imageView.center.x, imageView.center.y-200); }生命周期
// 提交动画 [UIView commitAnimations]; #endif事件
#if 0 #pragma mark -- block实现 /* 经过block回调动画 NSTimeInterval 动画的持续时间 */ [UIView animateWithDuration:1.0 animations:^{ // 只须要将执行动画的代码块添加到这 if ((sender.tag = !sender.tag)) { // 奇数次点击button进入到这个if语句 // 改变imageView的y坐标 imageView.center = CGPointMake(imageView.center.x, imageView.center.y+200);
}else{ // 偶数次点击进入else imageView.center = CGPointMake(imageView.center.x, imageView.center.y-200); } }];
#endif /* 动画 */ [UIView animateWithDuration:1.0 animations:^{ // 将要执行动画的代码添加到这里
if ((sender.tag = !sender.tag)) { // 奇数次点击button进入到这个if语句 // 改变imageView的y坐标 imageView.center = CGPointMake(imageView.center.x, imageView.center.y+200); }else{ // 偶数次点击进入else imageView.center = CGPointMake(imageView.center.x, imageView.center.y-200); } } completion:^(BOOL finished) { // 动画执行完成会执行这里的代码 NSLog(@"动画已经执行完成"); }];
} // 交换两个imageView的位置
// 一、建立对象 CATransition * transition = [CATransition animation]; // 二、设置动画持续时间 [transition setDuration:1.0]; // 三、设置动画的执行效果
#if 0 /* Common transition types. */
CA_EXTERN NSString * const kCATransitionFade __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionMoveIn __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionPush __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionReveal __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); /* Common transition subtypes. */ CA_EXTERN NSString * const kCATransitionFromRight __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionFromLeft __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionFromTop __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); CA_EXTERN NSString * const kCATransitionFromBottom __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0); 1. pageCurl 向上翻一页 2. pageUnCurl 向下翻一页 3. rippleEffect 滴水效果 4. suckEffect 收缩效果,如一块布被抽走 5. cube 立方体效果 6. oglFlip 上下翻转效果 7. .moveIn 8. .@"fade"(default)
#endif transition.type = @"suckEffect"; //[transition setType:kCATransitionMoveIn]; // 四、设置动画执行时的方法 transition.subtype =kCATransitionFromTop; // 五、将transition添加到self.view的layer上面 [self.view.layer addAnimation:transition forKey:nil]; [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; @end
// 视图的生命周期()方法一 // 建立对象时调用,只会被调用一次
(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { self.view.backgroundColor = [UIColor magentaColor]; NSLog(@"%s",func); // 打印方法 // 之后写代码,不能今后方法中设置编写与视图有关的代码,会提早加载视图,打乱控制器的生命周期。
} return self; }
// 方法二 // 加载视图时调用,只会被调用一次
// 方法三 // 用于显示数据,或者再手动加载本身想要加载的视图,只会被调用一次
//方法四 // 当视图将要出现时调用,会被屡次调用
// 方法八 // 当控制器对象将要销毁时调用,只会被调用一次
(void)dealloc{
NSLog(@"%s",func);
NSLog(@"注册页面即将被销毁"); [_userNameText release]; [_pwdText release]; [super dealloc]; }
@end
// 手势 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
// 点击次数,默认为1 [tap setNumberOfTapsRequired:2]; // 设置手指的个数 [tap setNumberOfTouchesRequired:2];
// 将手势添加到_imageView上面 [_imageView addGestureRecognizer:tap]; [tap release]; //UILongPressGestureRecognizer 长按 UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; [_imageView addGestureRecognizer:longPress]; longPress.minimumPressDuration = 1; [longPress release];
//UISwipeGestureRecognizer 上下左右 UISwipeGestureRecognizer * swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[_imageView addGestureRecognizer:swipeLeft]; [swipeLeft release]; UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; [_imageView addGestureRecognizer:swipeRight]; [swipeRight release];
//UIPanGestureRecognizer 移动 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; [_imageView addGestureRecognizer:pan]; [pan release];
//UIPinchGestureRecognizer 放大缩小 // UIPinchGestureRecognizer * pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; // [_imageView addGestureRecognizer:pin]; // [pin release];
//UIRotationGestureRecognizer 旋转 UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; [_imageView addGestureRecognizer:rotation]; [rotation release];
}
(void)rotation:(UIRotationGestureRecognizer *)sender{ if (sender.state == UIGestureRecognizerStateChanged) { // 只能旋转一次 // [sender.view setTransform:CGAffineTransformMakeRotation(sender.rotation)];
[sender.view setTransform:CGAffineTransformRotate([sender.view transform], sender.rotation)];
}
}
(void)pinch:(UIPinchGestureRecognizer *)pinch{ NSLog(@"%s",func); if (UIGestureRecognizerStateChanged == pinch.state) { // 以scale为参数放大或者缩小多少倍 [pinch.view setTransform:CGAffineTransformScale([pinch.view transform], pinch.scale, pinch.scale)]; // 手势完成 }else if (UIGestureRecognizerStateEnded == pinch.state){ [_imageView setTransform:CGAffineTransformIdentity]; } }
(void)pan:(UIPanGestureRecognizer *)pan{
NSLog(@"%s",func); // 判断手势的状态是否是在变化,相似于touchesMoved: if (UIGestureRecognizerStateChanged == pan.state) { // 经过手势实例的translationInView方法能够得到变化先后两个位置的x和y的差 CGPoint deltaPoint = [pan translationInView:self.view]; // 传入x和y先后 之差,经过图形变化实现平移 NSLog(@"%@",NSStringFromCGPoint(deltaPoint)); [_imageView setTransform:CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y)]; } }
(void)swipe:(UISwipeGestureRecognizer *)swipe{ switch (swipe.direction) { case UISwipeGestureRecognizerDirectionUp: NSLog(@"Up"); break; case UISwipeGestureRecognizerDirectionDown: NSLog(@"Down"); break; case UISwipeGestureRecognizerDirectionLeft: NSLog(@"Left"); break; case UISwipeGestureRecognizerDirectionRight: NSLog(@"Right"); break; default: break; }
}
(void)tap:(UITapGestureRecognizer *)tap{ // 获取到我点击的视图 UIView * view = tap.view;
NSLog(@"%s",func); }
(void)longPress:(UILongPressGestureRecognizer *)longPress{
// NSLog(@"%ld",longPress.state);
// 长按时,若是手指滑动可能这个方法会被屡次调用,咱们添加一个判断,当长按手势结束时,才去处理咱们想要作的事情 if (longPress.state == UIGestureRecognizerStateChanged) { NSLog(@"%s",func); }
}
@end
UIButton控件
// enabled 禁用属性 button1.enabled =YES; // 选中状态属性,默认值为NO button1.selected = NO; // 设置文字大小 button1.titleLabel.font = [UIFont systemFontOfSize:10.0];
// 程序内存警告时才会调用
//(UIImageView) 由名字直接读取 // 优势:相对找到图片的时间较快,消耗相对较高 // 缺点:这张图片不会销毁,相对比较消耗内存 UIImage * image = [UIImage imageNamed:@"map.png"]; UIImage * imageJpg = [UIImage imageNamed:@"q.jpg"]; // 实例化UIImageView类 UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 40, 100, 80)]; // Image属性:展现图片的属性 // imageView.image = image; imageView.image = imageJpg; // 由路径直接读取图片 // 优势:找到以后,用完以后就会销毁,不会占用特别多的内存 // 缺点:找到图片的时间相对较慢,效率较低。 // 找到一个文件的路径 NSString * path = [[NSBundle mainBundle]pathForResource:@"map" ofType:@"png"]; // 由路径直接读取图片:imageWithContentsOfFile:参数写路径的字符串形式 UIImage * imagePath = [UIImage imageWithContentsOfFile:path]; UIImageView * imageViewPath = [[UIImageView alloc]initWithFrame:CGRectMake(30, 150, 260, 100)]; imageView.image = imagePath; [self.view addSubview:imageViewPath];
@end
//UILabel控件 lable.alpha = 0.5; lable.hidden = NO; // 设置文字颜色 lable.textColor = [UIColor greenColor]; // 设置文字字体 font:设置文本文字的大小 lable.font = [UIFont systemFontOfSize:24.0]; // 设置文字字体 font:设置文本文字的大小 有加粗效果 lable.font = [UIFont boldSystemFontOfSize:28.0]; // 设置文字字体 font:设置文本文字的大小 有文字斜体效果(只有英文有效) lable.font = [UIFont italicSystemFontOfSize:18.0]; // 设置文字的对齐方式 textAlignment lable.textAlignment = NSTextAlignmentCenter; lable.textAlignment = NSTextAlignmentLeft; //lable.textAlignment = NSTextAlignmentRight; // 设置行数 大于0的数 lable.numberOfLines = 0; //adjustsFontSizeToFitWidth : 自适应文字大小,以前设置的文字大小无效,只适用于一行的文本 //lable.adjustsFontSizeToFitWidth = YES;
// 自适应文本文字的高度,须要将numberOfLines这个属性的值等于0 以前设置的高度无效 [lable sizeToFit];
//用代码去自适应UILable UIFont * font = [UIFont systemFontOfSize:24.0]; // 建立一个字典:NSFontAttributeName->UIFont NSDictionary * fontDict = @{NSFontAttributeName:font}; // CGRect // CGSize -> CGSizeMake(width,height) -> 第一个参数写lable的宽度;第二个参数:MAXFLOAT(苹果默认的极大的数) // 第二个参数:NSStringDrawingUsesLineFragmentOrigin // 第三个参数:关于字体大小的字典 // 第四个参数:nil CGRect lableRect = [str boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil]; // CGRect(结构体) -> CGSize(结构体) -> width,height //CGSize CGSize lableSize = lableRect.size; // height CGFloat lableHeight = lableSize.height;
UILabel * lable1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, lableHeight)]; lable1.backgroundColor = [UIColor greenColor]; lable1.text = str; lable1.font = font; lable1.numberOfLines = 0; [self.window addSubview:lable1];
@end