iOS學習(UI)知識點整理
一、關于UIButton的介紹
1)概念:UIButton 是一種常用的控件,通過點擊觸發相應的功能
2)UIButton 的幾種常用的狀態
1、UIControlStateNormal 正常狀態
2、UIControlStateHighlighted 高亮狀態
3、UIControlStateSelected 選中狀態 -> 當button的selected設置成yes之后才能觸發
3)UIButton常用的幾種事件
1、UIControlEventTouchUpInside 按鈕按下并抬起事件
2、UIControlEventTouchDown 按鈕按下事件
3、UIControlEventTouchDownRepeat 按鈕多次點擊觸發事件
4)UIButton 初始化實例代碼
1 UIButton *button = [[UIButton alloc] init]; 2 button.frame = CGRectMake(20, 50, 50 , 50); 3 button.backgroundColor = [UIColor clearColor]; 4 [button setTitle:@"按鈕1 正常狀態" forState:UIControlStateNormal]; 5 [button setTitle:@"按鈕1 高亮狀態" forState:UIControlStateHighlighted]; 6 [button setTitle:@"按鈕1 選中狀態" forState:UIControlStateSelected]; 7 8 //按鈕點擊時觸發事件 9 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];10 //按鈕按下后觸發事件11 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDown];12 //按鈕雙擊觸發事件13 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDownRepeat];14 //設置按鈕高亮狀態下的字體顏色15 [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];16 //button字體變為35號加粗的字體17 button.titleLabel.font = [UIFont boldSystemFontOfSize:35];18 //設置圓角 19 button.layer.cornerRadius = 5.f;20 //設置邊框寬度21 button.layer.borderWidth = 2.1;22 //設置邊框顏色23 button.layer.borderColor = [UIColor lightGrayColor].CGColor;24 //設置按鈕背景圖 25 UIImage *imageNormal = [UIImage imageNamed:@"camera"];26 //設置imageNormal為按鈕的正常情況的圖片27 [button setImage:imageNormal forState:UIControlStateNormal];28 29 UIImage *imageHightLight = [UIImage imageNamed:@"camera2"];30 //設置imageHightLight為按鈕的高亮情況的圖片31 [button setImage:imageHightLight forState:UIControlStateHighlighted];32 //當button設置了圖片的時候 并且沒有設置高亮狀態下得圖片,取消高亮狀態, 默認是Yes33 button.adjustsImageWhenHighlighted = YES; 34 [self.window addSubview:button];
5)防止按鈕多次點擊重復提交數據的實例代碼
1 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 3 - (void)buttonTapped:(UIButton *)button 4 { 5 //設置按鈕不可點擊 6 button.userInteractionEnabled = NO; 8 //延遲執行方法 防止按鈕被快速點擊或者不希望點擊造成錯誤 9 [self performSelector:@selector(delayMethod:) withObject:button afterDelay:1]; 11 }12 13 //延遲方法->設置按鈕為可點擊狀態14 - (void)delayMethod:(UIButton *)button15 {16 button.userInteractionEnabled = YES; 17 }
二、關于UIImageView的介紹
1)概念:UIImageView 是iOS中專門用于展示圖片的控件
2)UIImageView 初始化 實例代碼
1 UIImageView *imageView = [[UIImageView alloc] init]; 2 imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width); 3 imageView.backgroundColor = [UIColor whiteColor]; 4 imageView.center = self.view.center; 5 6 //tag設置控件的唯一標識,值不能重復 7 imageView.tag = 100; 8 9 //UIImageView的 clipsToBounds屬性,設置為yes的時候超出部分,不予以顯示10 imageView.clipsToBounds = YES;11 12 //讀取一張圖片13 UIImage *image = [UIImage imageNamed:@"icon"];14 imageView.image = image;15 16 //設置圖片展示模式17 imageView.contentMode = UIViewContentModeScaleaspectFill;18 19 //打開imageview的用戶交互 注:要實現圖片點擊事件此屬性必須設置為YES20 imageView.userInteractionEnabled = YES;21 [self.view addSubview:imageView];22 23 //為UIImageView添加點擊事件 24 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(imageViewTapped:)];
25 [imageView addGestureRecognizer:tap];
3)UI_ImageView中常用的幾種填充模式
1、UIViewContentModeScaleToFill 拉伸image使其充滿UIImageView
2、UIViewContentModeScaleAspectFill 拉伸image使其不變形,并且充滿UIImageView
3、UIViewContentModeScaleAspectFit 拉伸imgage使其不變形,并且完全顯示在UIImageView中
4)UITapGestureRecognizer 除了可以給UI_ImageView添加點擊方法外還可以給其他控件添加點擊方法
如:UI_Lable、UI_View...等
5)iOS中獲取圖片的三種方法
方法一:
1 //把圖片對象加載到內存中2 UIImage *image1 = [UIImage imageNamed:@"camera"];3 CGSize size = image1.size;4 NSLog(@"size.w %f size.h %f",size.width ,size.height);5 //如果圖片的格式是png,則后綴名可以省略,其他格式不能省略6 UIImage *image2 = [UIImage imageNamed:@"icon.jpeg"];
方法二:
//使用場景:讀取大圖片,比較占內存的,需要及時釋放的圖片要用這種方法 //讀取icon.jpegNSString *imagePath3 = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];UIImage *image3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];NSString *imagePath3_1 = [[NSBundle mainBundle] pathForResource:@"icon.jpeg" ofType:nil];UIImage *image3_1 = [[UIImage alloc] initWithContentsOfFile:imagePath3_1];
方法三:
1 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];2 3 UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];
新聞熱點
疑難解答