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

來源:互聯網
上載者:User

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

第七課:

  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課程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.