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. NSCoder Night
    [self SP];

    View full-size slide

  2. WHOAMI ?
    I’m Fernando a.k.a ferbass
    Senior iOS Developer at Akatus
    SplashCam iOSDevSpot
    Twitter @fer_bass
    [email protected]

    View full-size slide

  3. not in app store yet
    aiqfome sample app

    View full-size slide

  4. 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

    View full-size slide

  5. First we need
    understand views

    View full-size slide

  6. UIView
    - (void)drawRect:(CGRect)rect
    {
    !
    }

    View full-size slide

  7. UIView
    - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();
    }

    View full-size slide

  8. UIView
    - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();
    !
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
    }

    View full-size slide

  9. 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);
    }

    View full-size slide

  10. 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

    View full-size slide

  11. 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);
    }

    View full-size slide

  12. 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);
    }

    View full-size slide

  13. 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);
    }

    View full-size slide

  14. 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);
    }

    View full-size slide

  15. Use your imagination

    View full-size slide

  16. Links
    http://www.raywenderlich.com/tag/core-graphics
    http://www.amazon.com/dp/0123694736/?
    tag=stackoverfl08-20
    http://google.com :P
    https://developer.apple.com/library/ios/documentation/
    coregraphics/reference/coregraphics_framework/
    CoreGraphics_Framework.pdf

    View full-size slide