Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 26th, 2012  |  syntax: None  |  size: 2.75 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. iOS: Drawing a CGImage into a subclass of CALayer during custom property animation in correct resolution
  2. #import "ScrollBarView.h"
  3. #import "ScrollBarLayer.h"
  4.  
  5. @implementation ScrollBarView
  6.  
  7. @synthesize sbl;
  8.  
  9. - (id)initWithFrame:(CGRect)frame
  10. {
  11.     self = [super initWithFrame:frame];
  12.     if (self) {
  13.         // Initialization code
  14.     }
  15.     return self;
  16. }
  17.  
  18. - (void)setImagesWithName:(NSString*)imageName {
  19.     ScrollBarLayer *ssbl = [[ScrollBarLayer alloc] init];
  20.     ssbl.frame = CGRectMake(0, 0, 30, 30);
  21.     [ssbl setImagesWithName:imageName];
  22.     [self.layer addSublayer:ssbl];
  23.  
  24.     self.sbl = ssbl;
  25.  
  26.     [ssbl release];
  27. }
  28.  
  29. - (void) dealloc {
  30.     self.sbl = nil;
  31.     [super dealloc];
  32. }
  33.  
  34. - (void)setPercentageWithFloat:(CGFloat)perc {
  35.     if(perc > 1.0){
  36.         perc = 1.0;
  37.     } else if(perc < 0) {
  38.         perc = 0;
  39.     }
  40.  
  41.     [self.sbl setPercentage:perc];
  42.  
  43.     CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"percentage"];
  44.     ba.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  45.     ba.duration = 0.8;
  46.     ba.toValue = [NSNumber numberWithFloat:perc];
  47.     [self.sbl addAnimation:ba forKey:nil];
  48. }
  49. @end
  50.        
  51. #import "ScrollBarLayer.h"
  52.  
  53. @implementation ScrollBarLayer
  54.  
  55. @synthesize filename;
  56. @dynamic percentage;
  57.  
  58. - (id)init {
  59.     self = [super init];
  60.     if (self) {
  61.  
  62.     }
  63.     return self;
  64. }
  65.  
  66. - (void)setImagesWithName:(NSString*)imageName {
  67.     self.frame = CGRectMake(0, 0, 30, 30);
  68.     self.percentage = 0;
  69.     self.filename = imageName;
  70. }
  71.  
  72. + (BOOL) needsDisplayForKey:(NSString*)key {
  73.     if([key isEqualToString:@"percentage"]) {
  74.         return YES;
  75.     }
  76.     return [super needsDisplayForKey:key];
  77. }
  78.  
  79. - (void) drawInContext:(CGContextRef)ctx {
  80.  
  81.     int imageIdx = (int)roundf((float)199 * self.percentage);
  82.     NSString *thisfilename = [self.filename stringByAppendingString:[NSString stringWithFormat:@"%03d.png", i+1]];
  83.     UIImage* c = [UIImage imageNamed:thisfilename];
  84.     CGImageRef img = [c CGImage];
  85.     CGSize sz = CGSizeMake(CGImageGetWidth(img), CGImageGetHeight(img));
  86.  
  87.     UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 0.0);
  88.     CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width, sz.height), img);
  89.     UIGraphicsEndImageContext();
  90. }
  91.  
  92. - (void) dealloc {
  93.     self.pictures = nil;
  94.     self.filename = nil;
  95.     [super dealloc];
  96. }
  97.  
  98. @end
  99.        
  100. #define IS_RETINA_DISPLAY() ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00)
  101.  
  102. ...
  103. UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 0.0);
  104.  
  105. if (IS_RETINA_DISPLAY())
  106. {
  107.     CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width/2.0f, sz.height/2.0f), img);
  108. } else
  109. {
  110.     CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width, sz.height), img);
  111. }
  112. UIGraphicsEndImageContext();
  113. ...