Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A positividade do CoreGraphics

A positividade do CoreGraphics

CoreGraphics é um framework bem poderoso, porém mal compreendido por todos nós, sempre temos medo dele e acabamos usando alternativas. Vou mostrar como o CoreGraphics pode ser usado de maneira simples com pequenos truques sem utilizar geradores de código.

Fernando Bass

December 10, 2013
Tweet

More Decks by Fernando Bass

Other Decks in Technology

Transcript

  1. Advantages in CoreGraphics Custom UI with native API No need

    images to create a custom UI Reduce app size Create a reusable classes for your UI
  2. UIView - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); ! CGContextSetStrokeColorWithColor(context,

    [[UIColor grayColor] CGColor]); ! CGContextMoveToPoint(context, CGRectGetMinX(rect) + 15, CGRectGetMaxY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect) - 15, CGRectGetMaxY(rect)); ! CGContextStrokePath(context); }
  3. CG_EXTERN void CGContextSetLineDash(CGContextRef c, CGFloat phase, const CGFloat lengths[], size_t

    count) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); Create a dash line
  4. UIView - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); ! CGContextSetStrokeColorWithColor(context,

    [[UIColor grayColor] CGColor]); ! CGFloat dashes[] = {1,1}; CGContextSetLineDash(context, 0.0, dashes, 2); ! CGContextMoveToPoint(context, CGRectGetMinX(rect) + 15, CGRectGetMaxY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect) - 15, CGRectGetMaxY(rect)); ! CGContextStrokePath(context); }
  5. View with dotted border - (void)drawRect:(CGRect)rect { CGContextRef context =

    UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [[UIColor grayColor] CGColor]); CGFloat dashes[] = {1,1}; CGContextSetLineDash(context, 0.0, dashes, 2); CGContextSetLineWidth(context, 1.0); CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect)); CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)); CGContextSetShouldAntialias(context, NO); CGContextStrokePath(context); }
  6. ZigZag - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [[UIColor

    colorWithRed:241.0/255.0 green: 241.0/255.0 blue:238.0/255.0 alpha:1] CGColor]); ! CGContextSetLineWidth(context, 0.6); CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect) - 5); CGContextSetShouldAntialias(context, YES); ! for (int i = 0; i < 320; i++) { float x = (CGRectGetMinX(rect) + 7) * i; float y = CGRectGetMaxY(rect) - 5; CGContextAddLineToPoint(context, x, y); CGContextAddLineToPoint(context, x + 1, y); ! CGContextAddLineToPoint(context, x + 4, CGRectGetMaxY(rect)); CGContextSetShouldAntialias(context, NO); } ! CGContextStrokePath(context); }
  7. Custom text fields - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, [[UIColor colorWithRed:0.553 green:0.561 blue 0.565 alpha:1.000] CGColor]); CGFloat dashes[] = {1,1}; CGContextSetLineDash(context, 0.0, dashes, 1); CGContextSetLineWidth(context, 1.0); CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect)); CGContextSetShouldAntialias(context, NO); CGContextStrokePath(context); }