UIKit 手寫控件 UIImageView 和UITableView

UIImageView 是用來放置圖片的性能

建立⼀一個UIImageView對象有五種⽅方法:
1.UIImageView *imageView1 = [[UIImageView alloc] init]; 實例化了一個UIImageView類型的對象 
2. UIImageView *imageView2 = [[UIImageView alloc] initWithFrame: (CGRect)]; 實例化了一個UIImageView類型的對象同時設置了圖片的位置
3.UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];實例化了一個UIImageView類型的對象同時設置了圖片是哪個圖片對象spa

不經常使用
1. UIImageView *imageView4 = [[UIImageView alloc] initWithImage: (UIImage *) highlightedImage:(UIImage *)];當這個ImageView的highlighted高亮的屬性是 YES時,顯示的就是參數highlightedImage高亮的,通常狀況下顯示的是第一個參數UIImage 
2. UIImageView *imageView5 = [[UIImageView alloc] initWithCoder: (NSCoder *)];代理

當以後想改變位置時,能夠從新設定frame屬性:
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
注意到UIImageView還有一個bounds屬性:
imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);orm

frame設置其位置和大小,而bounds只能設置其大小,其參數中的x、y不起做用即使是以前沒有設定frame屬性,控件最終的位置也不是bounds所設定的參數。bounds實現的是將UIImageView控件以原來的中心爲中心進行縮放。
例如 以下代碼:
imageView.frame = CGRectMake(0, 0, 320, 460); imageView.bounds =
CGRectMake(100, 100, 160, 230); 執行以後,這個imageView的位置和大小是(80, 115, 160, 230)。對象

contentMode屬性:
這個屬性是用來設置圖片的顯示方式,如居中、居右,是否縮放等,有如下⼏個常量可供設定:
UIViewContentModeScaleToFill 
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill 
UIViewContentModeRedraw 
UIViewContentModeCenter 
UIViewContentModeTop 
UIViewContentModeBottom
UIViewContentModeLeft 
UIViewContentModeRight 
UIViewContentModeTopLeft
UIViewContentModeTopRight 
UIViewContentModeBottomLeft 
UIViewContentModeBottomRight繼承

更改位置:
更改一個UIImageView的位置,能夠直接修改其frame屬性
修改其center屬性:
imageView.center = CGPointMake(CGFloat x, CGFloat y); center屬性指的就是這個ImageView的中間點。
使用transform屬性
imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy); 其中dx與dy表示想要往x或者y方向移動多少,而不是移動到多少。事件

旋轉圖像:
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照順時針方向旋轉的,⽽且旋轉中心是原始ImageView的中心,
也就是center屬性表⽰示的位置。
這個⽅法的參數angle的單位是弧度,而不是咱們最經常使用的度數,因此能夠寫一
個宏定義:
#define degreesToRadians(x) (M_PI*(x)/180.0) 其中x寫要旋轉的角度圖片

縮放圖像:
仍是使用transform屬性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
其中,CGFloat scale_w與CGFloat scale_h分別表⽰將原來的寬度和高度縮放到多少倍get

播放⼀系列圖片:
imageView.animationImages = imagesArray; // 設定全部的圖片在多少秒內播放完畢 imageView.animationDuration = [imagesArray count]; // 不重複播放多少遍,0表示無數遍 imageView.animationRepeatCount = 0; // 開始播放 [imageView startAnimating];animation

爲圖片添加單擊事件:
imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap]; 必定要先將userInteractionEnabled置爲YES,這樣才能響應單擊事件。

其餘設置:
imageView.hidden = YES或者NO; // 隱藏或者顯示圖片 imageView.alpha = (CGFloat) al; // 設置透明度 imageView.highlightedImage = (UIImage *)hightlightedImage; // 設置⾼亮時顯示的圖片 imageView.image = (UIImage *)image; // 設置正常顯示的圖片 [imageView sizeToFit]; // 將圖⽚尺寸調整爲與內容圖片相同

UIButton和UIImageView的區別:
1> UIImageView只能一種圖片(圖片默認會填充整個UIImageView) image\setImage: 
2> UIButton能顯示2種圖片
背景(背景會填充整個UIButton) setBackroungImage:forState:
前置(覆蓋在背景上面的圖片,按照以前的尺寸顯示) setImage:forState: * 還能顯⽰文字

1> UIImageView默認是不能響應點擊事件
2> UIButton能響應點擊事件 : addTarget:action:forControlEvents:

1> UIImageView : 只顯⽰圖片,不監聽點擊,點擊了圖片後不作任何反應 
2> UIButton : 既顯示圖片,又監聽點擊,點擊了圖片後作一些其餘事情

1> UIButton之因此能添加監聽器來監聽事件,是由於它繼承自UIControl
2> UIImagevIew之因此不能添加監聽器來監聽事件,是由於它直接繼承自UIView

UITableView

幾乎大多數的IOS項目中均可以看獲得UITableView的影子,UITableView是 iPhone中比較經常使用的,⽤的比較多的控件。主要是用於展現一系列表的數據。
特色:顯示大型內容的列表,單行,多列,垂直滾動,沒有水平滾動,大量的數據集,性能強大。
UITableView有兩個默認的內置風格 一個是簡明風格 UITableViewStylePlain。 另外一個是組團風格UITableViewStyleGrouped

UITableView有兩個代理協議

Protocol UITableViewDataSource:⽤來給TableView提供數據
什麼是data source:數據的來源。⼀般狀況下咱們會設置擁有 UITableView的這個UIViewController爲他的data source。
Protocal UITableViewDelegate:控制TableView的展現方式以及事件響應

協議實現:

//設置分區高度-(NSString*)tableView:(UITableView*)tableView:titleForHeaderInSection: (NSInteger)section

//改變行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath

//行縮進
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath: (NSIndexPath *)indexPath 

相關文章
相關標籤/搜索