IOS CALayer(一),ioscalayer

來源:互聯網
上載者:User

IOS CALayer(一),ioscalayer

對於一個app的好壞,我們首要判斷的便是app的介面,而介面的建立則是在圖形的處理基礎上的,說到圖形處理又不得不提及Quartz2D,CALayer。

在iOS系統中,你能看得見摸得著的東西基本上都是UIView,比如一個按鈕、一個文字標籤、一個文本輸入框、一個表徵圖等等,這些都是UIView。

其實UIView之所以能顯示在螢幕上,完全是因為它內部的一個層。

在建立UIView對象時,UIView內部會自動建立一個層(即CALayer對象),通過UIView的layer屬性可以訪問這個層。當UIView需要顯示到螢幕上時,會調用drawRect:方法進行繪圖,並且會將所有內容繪製在自己的層上,繪圖完畢後,系統會將層拷貝到螢幕上,於是就完成了UIView的顯示。

 換句話說,UIView本身不具備顯示的功能,是它內部的層才有顯示功能。

首先展示一下無任何操作的imageView。

////  ViewController.m//  CX - CALayer(一)////  Created by ma c on 16/3/19.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 400)];        imageView.image = [UIImage imageNamed:@"nvshen.jpg"];        [self.view addSubview:imageView];}@end

設定陰影的效果

 

////  ViewController.m//  CX - CALayer(一)////  Created by ma c on 16/3/19.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 400)];        imageView.image = [UIImage imageNamed:@"nvshen.jpg"];    //設定陰影的顏色    imageView.layer.shadowColor = [UIColor orangeColor].CGColor;    //設定陰影的位移量    imageView.layer.shadowOffset = CGSizeMake(5, 5);    //設定陰影的透明度,1為不透明。    imageView.layer.shadowOpacity = 0.5;    [self.view addSubview:imageView];}@end

 

設定圓角的效果

////  ViewController.m//  CX - CALayer(一)////  Created by ma c on 16/3/19.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 400)];        imageView.image = [UIImage imageNamed:@"nvshen.jpg"];//    //設定陰影的顏色//    imageView.layer.shadowColor = [UIColor orangeColor].CGColor;//    //設定陰影的位移量//    imageView.layer.shadowOffset = CGSizeMake(5, 5);//    //設定陰影的透明度,1為不透明。//    imageView.layer.shadowOpacity = 0.5;    //設定圓角的半徑    imageView.layer.cornerRadius= 10;    //使視圖支援圓角    imageView.layer.masksToBounds = YES;    //masksToBounds 設定為YES 陰影製作效果將失效。        [self.view addSubview:imageView];}@end

 

設定邊框的效果

////  ViewController.m//  CX - CALayer(一)////  Created by ma c on 16/3/19.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 400)];        imageView.image = [UIImage imageNamed:@"nvshen.jpg"];//    //設定陰影的顏色//    imageView.layer.shadowColor = [UIColor orangeColor].CGColor;//    //設定陰影的位移量//    imageView.layer.shadowOffset = CGSizeMake(5, 5);//    //設定陰影的透明度,1為不透明。//    imageView.layer.shadowOpacity = 0.5;//    //設定圓角的半徑//    imageView.layer.cornerRadius= 10;//    //使視圖支援圓角//    imageView.layer.masksToBounds = YES;//    //masksToBounds 設定為YES 陰影製作效果將失效。    //設定邊框的寬度    imageView.layer.borderWidth = 5;    //設定邊框的顏色    imageView.layer.borderColor = [UIColor orangeColor].CGColor;        [self.view addSubview:imageView];}@end

 

設定旋轉的效果

////  ViewController.m//  CX - CALayer(一)////  Created by ma c on 16/3/19.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 400)];        imageView.image = [UIImage imageNamed:@"nvshen.jpg"];//    //設定陰影的顏色//    imageView.layer.shadowColor = [UIColor orangeColor].CGColor;//    //設定陰影的位移量//    imageView.layer.shadowOffset = CGSizeMake(5, 5);//    //設定陰影的透明度,1為不透明。//    imageView.layer.shadowOpacity = 0.5;//    //設定圓角的半徑//    imageView.layer.cornerRadius= 10;//    //使視圖支援圓角//    imageView.layer.masksToBounds = YES;//    //masksToBounds 設定為YES 陰影製作效果將失效。//    //設定邊框的寬度//    imageView.layer.borderWidth = 5;//    //設定邊框的顏色//    imageView.layer.borderColor = [UIColor orangeColor].CGColor;    //設定旋轉角度    //參數分別為,旋轉角度,旋轉軸 x y z    imageView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);        [self.view addSubview:imageView];}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.