_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];
@end
// 往导航栏的左边添加一个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
// 1、隐藏状态栏 [application setStatusBarHidden:NO]; // 如何隐藏状态栏 //1、需要在info.plist中添加一个字段,View controller-based status bar appearance,并且设置为NO // 2、在本方法中添加一句代码,代码为[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 backgrounds
UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
#endif
// 改变状态栏背景颜色的步骤?/* 1、View controller-based status bar appearance,并且设置为NO 2、本方法中添加一行代码[application setStatusBarStyle:UIStatusBarStyleLightContent]; */
@end
动画的实现 // button点击事件触发方法
-
(void)buttonClick:(UIButton *)sender{ // 如何通过tag获取子view UIImageView * imageView = (UIImageView *)[self.window viewWithTag:500]; #if 0 #pragma mark --注释代码 // 1、开始动画 [UIView beginAnimations:nil context:NULL]; // 2、设置动画持续时间 [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的位置
// 1、创建对象 CATransition * transition = [CATransition animation]; // 2、设置动画持续时间 [transition setDuration:1.0]; // 3、设置动画的执行效果
#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. .moveIn8. .@"fade"(default)
#endif transition.type = @"suckEffect"; //[transition setType:kCATransitionMoveIn]; // 4、设置动画执行时的方法 transition.subtype =kCATransitionFromTop; // 5、将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)loadView{ [super loadView]; NSLog(@"%s",func); }
// 方法三 // 用于显示数据,或者再手动加载自己想要加载的视图,只会被调用一次
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%s",func); }
//方法四 // 当视图将要出现时调用,会被多次调用
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@"%s",func); } // 方法五 // 当视图已经完全出现时被调用,会被多次调用
- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; NSLog(@"%s",func); } // 方法六 // 当视图将要看不见时调用,会被多次调用
- (void)viewWillDisappear:(BOOL)animated{ NSLog(@"%s",func); [super viewWillDisappear:animated]; } // 方法七 // 当视图已经完全看不见时调用,会被多次调用
- (void)viewDidDisappear:(BOOL)animated{ NSLog(@"%s",func); [super viewDidDisappear:animated]; }
// 方法八 // 当控制器对象将要销毁时调用,只会被调用一次
-
(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];
// 程序内存警告时才会调用
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
//(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