• IOS7笔记- 7、视图、绘制、手势识别


    1、字体设置

    #define CORNER_FON_STANDARD_HEIGHT 180.0
    -(CGFloat)cornerScaleFator { return self.bounds.size.height / CORNER_FON_STANDARD_HEIGHT;}
    UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFator]];

    2、段落设置

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;

    3、绘图

     1 - (void)drawRect:(CGRect)rect
     2  {
     3      // Drawing code
     4      UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds
     5                                                              cornerRadius:[self cornerRadius]];
     6      [roundedRect addClip];
     7      
     8      [[UIColor whiteColor] setFill];
     9      UIRectFill(self.bounds);
    10      
    11      [[UIColor blackColor] setStroke];
    12      [roundedRect stroke];
    13      
    14      if(self.faceUp){
    15          UIImage *faceImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [self rankAsString], self.suit]];
    16          if (faceImage) {
    17              CGRect imageRect = CGRectInset(self.bounds,
    18                                             self.bounds.size.width * (1.0 - self.faceCardScaleFactor),
    19                                             self.bounds.size.height * (1.0 - self.faceCardScaleFactor));
    20              [faceImage drawInRect:imageRect];
    21          } else {
    22              [self drawPips];
    23          }
    24          
    25          [self drawCorners];
    26      } else {
    27          [[UIImage imageNamed:@"cardback"] drawInRect:self.bounds];
    28      }
    29      
    30  }
     1 -(void)drawCorners
     2 {
     3     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
     4     paragraphStyle.alignment = NSTextAlignmentCenter;
     5     
     6     UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
     7     cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFator]];
     8     
     9     NSAttributedString *cornerText = [[NSAttributedString alloc]
    10                                       initWithString:[NSString stringWithFormat:@"%@
    %@", [self rankAsString], self.suit]
    11                                       attributes:@{NSFontAttributeName : cornerFont, NSParagraphStyleAttributeName :paragraphStyle}];
    12     CGRect textBounds;
    13     textBounds.origin = CGPointMake([self cornerOffset], [self cornerOffset]);
    14     textBounds.size = [cornerText size];
    15     [cornerText drawInRect:textBounds];
    16     
    17     CGContextRef context = UIGraphicsGetCurrentContext();
    18     CGContextTranslateCTM(context, self.bounds.size.width, self.bounds.size.height);
    19     CGContextRotateCTM(context, M_PI);
    20     [cornerText drawInRect:textBounds];
    21 }

    5、添加手势有两种方法:一种是对指定view中拖入手势控件,再由控件Ctrl成相应的Action实现。另一种是先在view相关的Controller中实现公共手势方法,在主View中添加手势事件,如下:

     1 //子View中实现公共手势方法(缩放手势)
     2 -(void)pinch:(UIPinchGestureRecognizer *)gesture
     3 {
     4     if((gesture.state == UIGestureRecognizerStateChanged ||
     5         gesture.state == UIGestureRecognizerStateEnded)) {
     6         self.faceCardScaleFactor *= gesture.scale;
     7         gesture.scale = 1.0;
     8     }
     9 }
    10 //主View中添加手势事件
    11 - (void)viewDidLoad
    12 {
    13     [super viewDidLoad];
    14     // Do any additional setup after loading the view, typically from a nib.
    15     [self.playingCardView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.playingCardView action:@selector(pinch:)]];
    16 }
  • 相关阅读:
    JS原生隐士标签扩展
    Asp.Net MVC强类型页面获取值几种方式
    Asp.Net MVC控制器获取视图传值几种方式
    vs2015 创建MVC项目
    C#后台HttpWebRequest模拟跨域Ajax请求,注册Windows服务到服务器上
    EF简介及CRUD简单DEMO
    Impala学习--代码生成(Code Generation)
    Impala源代码分析(2)-SQL解析与执行计划生成
    Impala源代码分析(3)-backend查询执行过程
    Impala源代码分析(1)-Impala架构和RPC
  • 原文地址:https://www.cnblogs.com/jonathan236/p/5567546.html
Copyright © 2020-2023  润新知