IOS開發之UI設計---視圖交互與事件(UIButton,UIImage,UIImageView,UIController)
沙盒 : IOS給每一個App分配一定的獨立的存儲空間
//獲取沙盒路徑
NSString *path = NSHomeDirectory();
如上圖所示的文件夾,即爲沙盒.沙盒空間彼此獨立,數據私有
異步:可以同時進行多個事件的進程(非阻塞).
異步事件模型:程序無法知道用戶何時出發應用的監聽機制,用戶一旦觸發,
應用立即做出響應.
UIButton : (UIControl : UIView)
+ (id)buttonWithType:(UIButtonType)buttonType;
- (void)setFrame:(CGRect)rect;
- (void)setTitle:(NSString )title forState:UIControlState)state;
// default is nil. title is assumed to be single line
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//target 添加事件的對象
//action 添加事件的響應方法
//controlEvents 事件的類型
//SEL類型:IOS系統會給每一個方法分配一個唯一的id,方法名一樣,id也一樣;
//@selector(click) 方法選擇器,將方法名click轉化爲id,類似C中的函數指針
ControlEvents & ControlState
ControlEvents未觸發,ControlState爲normal狀態,當ControlEvents 觸發時,ControlState纔會發生改變.
btn.tag = (NSInteger)tag;//給btn按鈕設置一個標籤
同步:一個事件的進程未完成,不能進行下一個事件的進程(阻塞).
UIImage : UIObject
UIImageView : UIView
UIImage 圖片數據的抽象類
UIImageView 展示UIImage的類,用於存放圖片信息的容器.
Method 1:通過文件名加載圖片
//從沙盒中讀取圖片
UIImage *image = [UIImage imageNamed:@"1.png"];
//實例化一個存放圖片的UIImageView容器
//UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 250, 100, 100)];
//---------------------
//圖片容器自適應 防止圖片失真
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectZero];
CGSize size = image.size;
[imgView setFrame:CGRectMake(200, 250, size.width, size.height)];
//加載圖片數據到圖片容器
[imgView setImage:image];
[self.window addSubview:imgView];
[imgView release];
imgView = nil;
Method 2:使用二進制數據流加載圖片
//獲取沙盒中文件的路徑
NSString *path = [[NSBundlemainBundle] pathForResource:@"2"ofType:@"png"];
//獲取圖片文件的二進制數據流對象
NSData *imgData = [NSData dataWithContentsOfFile:path];
//將圖片的二進制數據轉化爲UIImage
UIImage *image1 = [UIImage imageWithData:imgData];
CGSize size1 = image1.size;
UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, size1.width, size1.height)];
[imgView1 setImage:image1];
[self.window addSubview:imgView1];
[imgView1 release];
imgView1 = nil;
從代碼優化的角度,使用 + (UIImage *)imageWithName:(NSString *)filePath 會佔用系統的緩存空間,所以在加載圖片的時候,推薦使用
+ (UIImage *)imageWithData : (NSData *)data 來加載圖片.