_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