麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

(7/18)重學Standford_iOS7開發_視圖、繪制、手勢識別_課程筆記

2019-11-14 18:46:28
字體:
來源:轉載
供稿:網友

第七課:

  1、View

    一般來說,視圖是一個構造塊,代表屏幕上一塊矩形區域,定義了一個坐標空間,并在其中繪制及添加觸控事件等。

    ①視圖的層級關系

      一個視圖只能有一個父視圖,可以有多個子視圖

 1 - (void)addSubview:(UIView *)aView; // 父視圖添加子視圖 2 - (void)removeFromSuperview; // 子視圖從父視圖移除自己 

    ②UIWindow

      UIView的頂級視圖:一般情況下,iOS應用程序中只有一個UIWindow,指當前顯示的屏幕內容。

    ③UIView的初始化

      a.從storyboard中初始化:awakeFromNib

      b.代碼初始化:alloc initWithFrame:

- (void)setup { ... }- (void)awakeFromNib { [self setup]; }- (id)initWithFrame:(CGRect)aRect{    self = [super initWithFrame:aRect];    [self setup];    return self;}

     ④與視圖相關的類

      a.CGFloat

      b.CGPoint:(CGFloat)x,(CGFloat)y

      c.CGSize:(CGFloat)width,(CGFloat)height

      d.CGRect:(CGPoint)origin,(CGSize)size

    ⑤坐標系統

      a.像素與點的概念:每個View都有一個只讀屬性contentScaleFactor,用以標識一個點包含多少像素

      b.坐標系統屬性:(CGRect)bounds,(CGPoint)center,(CGRect)frame

        

        對于View B:  bounds = ((0,0),(200,250)) 

                frame = ((140,65),(320,320)) 

                center = (300,225) 

        此處理解視圖可以在父視圖中旋轉的概念。

    ⑥視圖的創建

      storyboard:drag

      code:alloc initWithFrame (直接使用init默認初始化為frame = CGRectZero)

1 CGRect labelRect = CGRectMake(20, 20, 50, 30);2 UILabel *label = [[UILabel alloc] initWithFrame:labelRect]; 3 label.text = @”Hello!”;4 [self.view addSubview:label];

     ⑦自定義視圖

      通過實現- (void)drawRect:(CGRect)aRect; 方法繪制內容,aRect指需要優化繪制的區域,與視圖最終性能有關(此處不作要求)

      注意:drawRect:方法不能主動調用,若需要重繪,可以調用- (void)setNeedsDisplay;或者- (void)setNeedsDisplayInRect:(CGRect)aRect;,系統會在合適的時間調用drawRect:

      a.drawRect的實現過程

        使用CoreGraphics: *獲取繪制內容的上下文

                  *創建繪制路徑(UIBezierPath)

                  *設置繪制屬性(color,font,textures,lineWidth,linecaps)

                  *描邊(strok),填充(fill)等

      b.UIBezierPath的使用

        UIBezierPath封裝好了上下文內容(上下文:指繪制的位置,內容等信息)

        UIKit調用DrawRect之前會處理好上下文內容,需要獲取當前上下文內容時使用:CGContextRef context = UIGraphicsGetCurrentContext(); 

UIBezierPath *path = [[UIBezierPath alloc] init];//創建//繪制路徑[path moveToPoint:CGPointMake(75, 10)];[path addLineToPoint:CGPointMake(160, 150)];[path addLineToPoint:CGPointMake(10, 150]);//閉合路徑[path closePath];//設置描邊和填充[[UIColor greenColor] setFill];[[UIColor redColor] setStroke];//描邊和填充[path fill]; [path stroke];
//其他用法 path.lineWidth = 2.0;//設置繪制路徑寬度UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:(CGRect)bounds cornerRadius:(CGFloat)radius];//繪制圓角矩形//繪制橢圓UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:(CGRect)bounds];//剪裁視圖[roundedRect addClip];//剪裁后的視圖只能在其路徑區域內繪制,超出部分不會繪制

      c.透明度相關

        *UIColor:屬性alpha(0.0-1.0)

        *UIView:(BOOL)opaque(不透明),alpha(0.0-1.0),hidden(隱藏視圖)

        區別請看:http://blog.csdn.net/martin_liang/article/details/40739845

      d.子視圖與父視圖轉換時上下文內容變化的問題

        壓入(push),取出(pop)狀態

- (void)drawGreenCircle:(CGContextRef)ctxt{    CGContextSaveGState(ctxt);//保存當前上下文    [[UIColor greenColor] setFill];    // draw my circle    CGContextRestoreGState(ctxt);//恢復保存的上下文}- (void)drawRect:(CGRect)aRect{    CGContextRef context = UIGraphicsGetCurrentContext();    [[UIColor redColor] setFill];    // do some stuff    [self drawGreenCircle:context];    // do more stuff and expect fill color to be red}

      e.繪制文本

        使用NSAttributeString

NSAttributedString *text = ...;//創建繪制內容CGSize textSize = [text size];//獲取文本尺寸大小[text drawAtPoint:(CGPoint)p];//將文本繪制到指定位置(左上角),或者使用drawInRect也可以

      f.繪制圖片

UIImage *image = [UIImage imageNamed:@“foo.jpg”];//UIImage *image = [[UIImage alloc] initWithContentsOfFile:(NSString *)fullPath];//UIImage *image = [[UIImage alloc] initWithData:(NSData *)imageData];//使用上下文繪制UIGraphicsBeginImageContext(CGSize);// draw with CGContext functionsUIImage *myImage = UIGraphicsGetImageFromCurrentContext();UIGraphicsEndImageContext();//標準繪制[image drawAtPoint:(CGPoint)p];//[image drawInRect:(CGRect)r];//[image drawaspatternInRect:(CGRect)patRect;

      g.bounds變化時視圖的重繪

        UIView屬性:@PRoperty (nonatomic) UIViewContentMode contentMode; 

//位置重繪UIViewContentMode{Left,Right,Top,Right,BottomLeft,BottomRight,TopLeft,TopRight}//縮放重繪UIViewContentModeScale{ToFill,AspectFill,AspectFit} // bit stretching/shrinking //bounds變化時調用drawRect重繪UIViewContentModeRedraw // it is quite often that this is what you want

  2、手勢識別

    步驟:a.創建手勢識別器,添加到視圖

       b.實現手勢觸發時的調用方法

    ①UIGestureRecognizer

      抽象超類,所有具體手勢類的父類

    ②添加手勢控制

- (void)setPannableView:(UIView *)pannableView // maybe this is a setter in a Controller{          _pannableView = pannableView;          UIPanGestureRecognizer *pangr =              [[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];//target也可是視圖控制器,pan為觸發時的調用方法,由target類實現          [pannableView addGestureRecognizer:pangr];//講手勢添加到視圖}

    ③pan手勢的例子

- (CGPoint)translationInView:(UIView *)aView;//觸摸移動的距離- (CGPoint)velocityInView:(UIView *)aView;//移動速度- (void)setTranslation:(CGPoint)translation inView:(UIView *)aView;

    ④抽象超類提供的state屬性

//UIGestureRecognizerStateBegin  連續手勢開始//UIGestureRecognizerStateChanged  移動//UIGestureRecognizerStateEnded//UIGestureRecognizerStateCancelled//UIGestureRecognizerStateFailed//UIGestureRecognizerStateRecognized     識別到手勢//使用舉例- (void)pan:(UIPanGestureRecognizer *)recognizer{    if ((recognizer.state == UIGestureRecognizerStateChanged) ||        (recognizer.state == UIGestureRecognizerStateEnded))     {        CGPoint translation = [recognizer translationInView:self];        // move something in myself (I’m a UIView) by translation.x and translation.y        // for example, if I were a graph and my origin was set by an @property called             origin self.origin = CGPointMake(self.origin.x+translation.x,     self.origin.y+translation.y);         [recognizer setTranslation:CGPointZero inView:self];//恢復手勢移動距離,為下次手勢識別調用初始化?    }}

    ⑤其他手勢屬性

//UIPinchGestureRecognizer 捏合手勢@property CGFloat scale; // 縮放比例@property (readonly) CGFloat velocity; //速度(readonly)UIRotationGestureRecognizer  旋轉手勢@property CGFloat rotation; // 旋轉弧度@property (readonly) CGFloat velocity; //速度(readonly)UISwipeGestureRecognizer  滑動手勢@property UISwipeGestureRecognizerDirection direction; //方向(4)@property NSUInteger numberOfTouchesRequired; // 觸控數量UITapGestureRecognizer  點擊手勢@property NSUInteger numberOfTapsRequired; // 點擊次數@property NSUInteger numberOfTouchesRequired; //觸控數量

  3、其他

    #pragma mark - example

    編譯器標記,對方法進行分組,結果如下

  5、demo

    SuperCard:https://github.com/NSLogMeng/Stanford_iOS7_Study/commit/1505f50229e875776c323fcd08d4b80e04cfcff0

 

 

課程視頻地址:網易公開課:http://open.163.com/movie/2014/1/2/A/M9H7S9F1H_M9H80ED2A.html

       或者iTunes U搜索standford課程


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久毛片免费观看 | wwwxxx国产 | 久久久婷婷一区二区三区不卡 | 欧美亚洲一区二区三区四区 | 亚洲第一综合色 | 一区视频 | 欧美一级高潮片免费的 | 中文字幕专区高清在线观看 | 91色一区二区三区 | 一级黄色国产视频 | 深夜视频在线观看 | 失禁高潮抽搐喷水h | 久久久精品网 | 性猛交ⅹxxx乱巴西 欧美日韩1区2区3区 | 青青草免费观看 | 日本欧美一区二区三区在线观看 | 草草视频免费观看 | 日日摸夜夜骑 | 最新在线黄色网址 | 舌头伸进添的我好爽高潮网站 | 成人综合在线观看 | 国产1区2 | 欧美一区二区精品夜夜嗨 | 日韩午夜一区二区三区 | 精品国产一区二区亚洲人成毛片 | 欧美一级无毛 | 国产69精品久久99不卡免费版 | 成人在线精品视频 | 日韩在线播放第一页 | 国产69精品久久久久久 | 黄色网址免费在线 | 亚洲网站在线观看视频 | 黄色网址免费在线播放 | 精品国产乱码一区二区三区四区 | 久久国产精 | 天天草夜夜骑 | 欧美成人se01短视频在线看 | 337p日本欧洲亚洲大胆精蜜臀 | 一区二区久久精品66国产精品 | 国产一级淫片在线观看 | 国产午夜免费视频 |