How to change iphone CATiledLayer fadeDuration?

2019-02-05 22:25发布

I am working on an iphone application that displays tiled maps. I am currently using a CATiledLayer in a UIScrollView :

     MyTiledDelegate *delegate=[[MyTiledDelegate alloc] initWithMapLayer:map];
     tileLayer = [CATiledLayer layer];
     tileLayer.delegate = delegate;
     [scrollView.layer addSublayer:tileLayer];
     [tileLayer setNeedsDisplay];   

I wrote and set my own delegate which implements the draw layer method like so :

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    CGRect rect =CGContextGetClipBoundingBox(ctx);
    CGFloat x = fabs(round(rect.origin.x/tileSize));
    CGFloat y = fabs(round(rect.origin.y/tileSize));

    Tile *tile = [map getTileForMapZoom:z x:x y:y];
    CGImageRef img=[tile getRealImage];
    CGContextDrawImage(
        ctx,
        CGRectMake(tile.x*tileSize,tile.y*tileSize, tileSize,tileSize) , 
        img);
    }//edited for brevity

I am annoyed by the default behavior of the CAtiledLayer to fadein after the tile is drawn. Also, sometimes the fadein is not complete (it stops at 90 or 95% opacity).

How can i change or (preferably) remove the fadein animation ?

I played with the speed and duration properties of my CATiledLayer instance, to no avail. I don't set any animation on the tiledLayer. the [tiledLayer removeAllAnimation] does not change anything either.

Thanks for any pointers.

3条回答
混吃等死
2楼-- · 2019-02-05 22:41

You should subclass the CATiledLayer and return fadeDuration of 0 to disable fade-in:

@interface FastCATiledLayer : CATiledLayer
@end

@implementation FastCATiledLayer
+(CFTimeInterval)fadeDuration {
  return 0.0;
}
@end

I also had the problem with fade in animation not completing, what helped was to set the background color of the view to [UIColor clearColor]

查看更多
闹够了就滚
3楼-- · 2019-02-05 22:55

Or in Swift 4:

class CAFastTiledLayer: CATiledLayer {
    class func fadeDuration() -> CFTimeInterval {
        return 0.0 
    }
}
查看更多
不美不萌又怎样
4楼-- · 2019-02-05 23:03

Try subclassing and overriding the +fadeDuration accessor on the layer.

查看更多
登录 后发表回答