Graphics Reference
In-Depth Information
The advantage of this is that you avoid the overhead of constantly creating and releasing
objects (which is expensive because it requires the allocation/deallocation of memory) and
you avoid having to re-apply properties that don't vary between instances.
Let's update our matrix example to use an object pool (see Listing 15.5).
Listing 15.5 Reducing Unnecessary Object Allocation by Recycling
@interface ViewController () <UIScrollViewDelegate>
@property ( nonatomic , weak ) IBOutlet UIScrollView *scrollView;
@property ( nonatomic , strong ) NSMutableSet *recyclePool;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//create recycle pool
self . recyclePool = [NSMutableSet set ];
//set content size
self . scrollView .contentSize = CGSizeMake (( WIDTH - 1 )* SPACING ,
( HEIGHT - 1 )* SPACING );
//set up perspective transform
CATransform3D transform = CATransform3DIdentity;
transform.m34 = - 1.0 / CAMERA_DISTANCE ;
self . scrollView .layer.sublayerTransform = transform;
}
- ( void )viewDidLayoutSubviews
{
[ self updateLayers ];
}
- ( void )scrollViewDidScroll:(UIScrollView *)scrollView
{
[ self updateLayers ];
}
- ( void )updateLayers
{
Search WWH ::




Custom Search