一些結(jié)構(gòu)體
//CGFloat:
//CGPoint: 有 CGFloat x 和 CGFloat y 兩個元素
//CGSize: 有 CGFloat width 和 CGFloat height 兩個屬性
//CGRect: 有 CGPoint origin 和 CGSize size 兩個屬性
View 的創(chuàng)建方法
//view 的創(chuàng)建方法
- (void)awakeFromNib;
- (void)init;
- (void)initWithFrame:(CGRect)aRect;
//view 的添加和移除方法
- (void)addSubview:(UIView *)aView;
- (void)removeFromSuperView;
//@property(nonatomic) CGRect bounds; //它描述了在它自己的坐標(biāo)系中的位置和大小
//@property(nonatomic) CGPoint center; //描述了父視圖的中心位置
//@property(nonatomic) CGRect frame; //它描述了在它在父視圖坐標(biāo)系統(tǒng)視圖的位置和大小
Custom Views
- (void)drawRect:(CGRect)aRect; //實現(xiàn)這個方法就可以繪制內(nèi)容, 這個方法是系統(tǒng)自動調(diào)用的, 不用手動調(diào)用(每次調(diào)用這個方法會刷新上下文)
- (void)setNeedsDisplay; //重新繪制視圖(它會調(diào)用 drawRect 方法)
- (void)setNeedsDisplayInRect:(CGRect)aRect; //傳進來的矩形會被重繪
//如何實現(xiàn) drawRect:?
方法是使用 Quartz 庫,叫做 Core Graphics(核心繪圖) \
Core Graphics 有很多 C 函數(shù),函數(shù)名以 CG 開頭, 幾乎都以 context上下文 作為第一個參數(shù) \
還可以使用 UIBezierPath 類, UIBezierPath類可以把各種復(fù)雜的形狀, 組成一個大的路徑 \
然后你可以在屏幕止對其進行描邊或填充 \
//Core Graphics(核心繪圖) 的基本流程 \
1.你需要有一個繪制的Context(上下文) \
2.你需要創(chuàng)建路徑 \
3.然后設(shè)置顏色,線寬,描邊,填充等屬性 \
4.對路徑進行描邊和填充 \
//UIBezierPath 封裝了上面4步的全部操作(其實也是操作上下文)
//1.如何獲得上下文? \
一般 UIKit 會在調(diào)用 drawRect: 前設(shè)置好上下文, 所以當(dāng)執(zhí)行到 drawRect: 時上下文就可以使用了 \
//CGContextRef context = UIGraphicsGetCurrentContext(); //如果在 drawRect: 中調(diào)用這個方法就可以獲取上下文 \
//2.如何定義路徑? \
UIBezierPath *path = [[UIBezierPath alloc] init]; //創(chuàng)建一個路徑(畫三角形) \
[path moveToPoint:CGPointMake(75,10)]; //移動到點 \
[path addLineToPoint:CGPointMake(160,150)]; //連線到點 \
[path addLineToPoint:CGPointMake(10.150)]; //連線到點 \
[path closePath]; //封閉路徑 \
//3.如何設(shè)設(shè)置顏色,線寬,描邊,填充等屬性?(除了描邊和填充其它的都用 UIBezierPath類 進行設(shè)置) \
//[[UIColor greenColor] set]; //調(diào)用 set 方法會把描邊和填充設(shè)置為一個顏色
[[UIColor greenColor] setFill]; //設(shè)置填充的顏色
[[UIColor redColor] setStroke]; //設(shè)置描邊的顏色
path.lineWidth = 2.0; //設(shè)置線寬為2
//4.如何進行描邊和填充? \
[path fill];
[path stroke];
UIBezierPath 的一些其它方法
//1.bezierPathWithRoundedRect: cornerRadius: 方法返回一個圓角矩形 \
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:(CGRect)bounds cornerRadius:(CGFloat)radius]; \
//2.使用 UIBezierPath 來裁剪繪圖 \
如果你想繪制某種圖案, 但是想讓它出現(xiàn)在一個圓角矩形內(nèi) \
你只要生成一個圓角矩形, 然后調(diào)用 addClip 方法 \
從這之后,你的所有繪圖都會被裁剪, 出現(xiàn)在那個圓角矩形路徑內(nèi), 并且還可以添加更多的裁剪區(qū)域 \
[roundedRect addClip]; \
//3.上下文的存檔與恢復(fù) \
CGContextSaveGState ( CGContextRef c ); //上下文的存檔 \
CGContextRestoreGState ( CGContextRef c ); //恢復(fù)到最近的存檔 \
//4.在上下文中繪制文本(屬性字符串), 調(diào)用 NSAttributedString 的 drawAtPoint方法 \
//創(chuàng)建屬性化字符串, 添加所需要的屬性
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"colinwang" attributes:@{}];
//調(diào)用 drawAtPoint: 方法,確定屬性化字符串的位置
[text drawAtPoint:(CGPoint)point];
//獲取文本所占空間的大小
CGSize textSize = [text size];
//5.繪制圖片是用到 UIImage 中的繪制方法
UIViewContentMode(view的一個屬性,它代表 bounds 變化時會發(fā)生什么)
文檔說明:當(dāng) view 大小改變時, 通過選項來校準(zhǔn)它的內(nèi)容
typedef enum {
UIViewContentModeScaleToFill,(默認,當(dāng) view.bounds 時通過拉伸像素來改變內(nèi)容)
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
UIGestureRecognizer(手勢)
//UIGestureRecognizer 是一個抽象類, 不能對它進行實例化
//手勢識別器的使用步驟 \
1.創(chuàng)建手勢識別器,并添加到視圖中 \
2.提供一個處理器,當(dāng)手勢發(fā)生時要調(diào)用的方法
//在輸出口添加手勢識別
- (void)setPannableView:(UIView *)pannableView{
_pannableView = pannableView;
//1創(chuàng)建拖動手勢
UIPanGestureRecognizer *pangr =
[[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];
//把手勢加入到目標(biāo)視圖中
[pannableView addGestureRecognizer:pangr];
}
- (void)pan:(UIPanGestureRecognizer *)recognizer{
if((recognizer.state == UIPanGestureRecognizerStateChanged) || (recognizer.state == UIPanGestureRecognizerStateEnded)){
//獲得拖動后的位置
CGPoint translation = [recognizer translationInView:self];
self.origin = CGPointMake(self.origin.x + translation.x, self.origin.y + translation.y);
//重置
[recognizer setTranslation:CGPointZero inView:self];
}
}
手勢動作
UIPanGestureRecognizer 拖動手勢
UIPinchGestureRecognizer 捏合手勢
UIRotationGestureRecognizer 旋轉(zhuǎn)手勢
UILongPressGestureRecognizer 長按手勢
UISwipeGestureRecognizer 滑動手勢(不連續(xù)手勢)
UITapGestureRecognizer 點擊手勢(不連續(xù)手勢)