[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/CoderLi-Q/Dev-Repo/master/iOSNote/iOS-coreAnimation.md [Back]  [Original]

# iOS -

#### [**,**](https://github.com/DevDragonLi/Dev-Repo/tree/master/iOSNote),,.

- [****](#performance)
- [****](#Efficient_rendering)
- [**IO**](#ImageIO)
- [****](#ThelayerPerformance)


## 1. 
				
#### iOSUIViewCALayer
- ****
	- iOSMac OSiOSUIKitUIViewMac OSAppKitNSView


- MaciPhoneiPadCore AnimationiOSMac OSOS

#### UIViewCALayer
- CALayer:
	- CALayerUIViewAPI
- 
- 3D
- 
- 
- 

## 2.

#### contents(:`id`,(CGImage/NSImage),CGImageRef:Core Foundation)

```
UIImage *imageContent =[UIImage imageNamed:@"back-LFL"];
// Xcode ,(__bridge id _Nullable)
Layer.contents = (__bridge id _Nullable)(imageContent.CGImage);

```
#### contentGravity:(CALayercontentModecontentsGravity,type: NSString)

```


kCAGravityCenter
kCAGravityTop
kCAGravityBottom
kCAGravityLeft
kCAGravityRight
kCAGravityTopLeft
kCAGravityTopRight
kCAGravityBottomLeft
kCAGravityBottomRight
kCAGravityResize
kCAGravityResizeAspect
kCAGravityResizeAspectFill

eg: CA_EXTERN NSString * const kCAGravityResizeAspectFill

```
#### contentsScale:,1.0

```
//set the contentsScale to match image
layerView.layer.contentsScale = image.scale;
// :
layer.contentsScale = [UIScreen mainScreen].scale;

```
#### maskToBounds:

#### contentsRect:()

- ****  iOSMac OSRetina2*2iOSRetina

- ****  UIImageCGImageRetina

- ****   OpenGLCore Animation

```
contentsRect{0, 0, 1, 1}.

+

```
#### contentsCenter:CGRect,contentsCenter
- :stretching  

#### Custom Drawing
- -drawRect: UIViewUIView-drawRect:  contentsScale


- CPU-drawRect:

- -drawRect:Core Graphics-setNeedsDisplaybounds-drawRect:UIViewCALayer

- CALayerdelegateCALayerDelegate()CALayer.
	- blueLayer-displayUIViewCALayer
	- masksToBoundsCALayerDelegate 

```
CALayer:
-(void)displayLayer:(CALayerCALayer *)layer;

contents-displayLayer:CALayer
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;

```

## 3.

#### :UIViewframeboundscenterCALayerframeboundsposition

-  frameboundspositiontransformframeframe

#### :anchorPoint

- {0, 0}{1, 1}{0.5, 0.5}

```
// 
self.imageView.layer.anchorPoint = CGPointMake(0.5f, 0.9f); 

```
#### ###

- UIViewCALayer(zPositionanchorPointZ)
- egCode:`    self.greenView.layer.zPosition = 1.0f;`

#### Hit Testing
- containsPoint : `if ([self.layerView.layer containsPoint:point])`
- hitTest	:CGPoint,

```
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get touch position
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //get touched layer
    CALayer *layer = [self.layerView.layer hitTest:point];
    //get layer using hitTest
    if (layer == self.blueLayer) {
  }  
}

```
#### 
- (void)layoutSublayersOfLayer:(CALayer *)layer;
-  bounds-setNeedsLayout,UIViewautoresizingMaskconstraints

##4. 

- :`self.layerView2.layer.cornerRadius = 20.0f;`
- :`self.layerView2.layer.borderWidth = 5.0f;`
- :
	- shadowColorborderColorbackgroundColorCGColorRef
	- shadowOffsetCGSizeshadowOffset {0, -3}Y3(:MacOS,)
	- shadowPath:CGPathRef

	```
	// 
	blueLayer.shadowOpacity = 0.9;
    blueLayer.shadowColor = [UIColor redColor].CGColor;
    blueLayer.shadowRadius = 10; // 
    blueLayer.shadowOffset = CGSizeMake(0, -7);
    
    //:CGPathRef
    CGMutablePathRef squarePath = CGPathCreateMutable();
    CGPathAddRect(squarePath, NULL,blueLayer.bounds);
    blueLayer.shadowPath = squarePath;
    CGPathRelease(squarePath);
    
	```
	 	
#### 

- 

```
 //create mask layer
  CALayer *maskLayer = [CALayer layer];
  maskLayer.frame = self.layerView.bounds;
  UIImage *maskImage = [UIImage imageNamed:@"back-LFL.png"];
  maskLayer.contents = (__bridge id)maskImage.CGImage;

  //apply mask to image layer
  self.imageView.layer.mask = maskLayer;
```
- 
	- kCAFilterLinear :()
	- kCAFilterNearest
	- kCAFilterTrilinear 

-  `opacity` (UIViewAlpha)
	- opacity ()
	- **CALayershouldRasterize4.7YES**

	```
  button.layer.shouldRasterize = YES;
  button.layer.rasterizationScale = [UIScreen mainScreen].scale;

	```

## 5.

## 6.

## 7.:

#### 
- Core AnimationCore Animation
- CALayer
- 

- CATransaction:+begin+commit

	- **Core Animationrun loop**run loopiOS**[CATransaction begin]run loop**0.25

	```
	// : Begin a new transaction for the current thread
	
	// : CATransaction+begin+commit+animateWithDuration:animations:block
    [CATransaction begin];
    
    [CATransaction setAnimationDuration:0.5];
    
    self.customlayer.backgroundColor =[UIColor redColor].CGColor;
    
    [CATransaction commit];
    
    [CATransaction setCompletionBlock:^{
    	// completionBlock    90
    	//rotate the layer 90 degrees
        CGAffineTransform transform = self.customlayer.affineTransform;
        transform = CGAffineTransformRotate(transform, M_PI_2);
	self.customlayer.affineTransform = transform;
    }];
    
	```
	
	- iOS4UIViewblock+animateWithDuration:animations:

#### 
##### Core AnimationCALayerUIView
- CALayerCALayer`-actionForKey:`

	- CALayerDelegate-actionForLayer:forKey
	- -actionForLayer:forKeyactions
	- actionsstyle
style-`defaultActionForKey:` 
	
- -actionForKey:CAActionCALayer


- **UIKit?**
	- UIView-actionForLayer:forKeyUIViewnilblock
  - UIViewnilCABasicAnimation
 -  CATransacition+setDisableActions:		

 - :
 
 ```
  //add a custom action
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    self.colorLayer.actions = @{@"backgroundColor": transition};
 ```

- :
	- UIViewUIViewCATransactionUIView-actionForLayer:forKey:
	- -actionForLayer:forKey:actions

	
	```
		LFLog(@"Inside: %@", [self.layerView actionForLayer:self.layerView.layer forKey:@"backgroundColor"]);

	```
#### :CALayer

- iOS6060Core Animation


## 8.
- CABasicAnimation
- CAAnimation

```
- (void)changeColor
{
    //create a new random color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    //create a basic animation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.toValue = (__bridge id)color.CGColor;
    animation.delegate = self;
    //apply animation to layer
    [self.colorLayer addAnimation:animation forKey:nil];
}

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag
{
    //set the backgroundColor property to match animation toValue
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.colorLayer.backgroundColor = (__bridge CGColorRef)anim.toValue;
    [CATransaction commit];
}

```
#### : CAKeyframeAnimation

- 

```
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.duration = 2.0;
    animation.values = @[
                         (__bridge id)[UIColor blueColor].CGColor,
                         (__bridge id)[UIColor redColor].CGColor,
                         (__bridge id)[UIColor greenColor].CGColor,
                         (__bridge id)[UIColor blueColor].CGColor ];
    //apply animation to layer
    [self.colorLayer addAnimation:animation forKey:nil];
```


## 12.

#### CPU VS GPU

- CPUGPUCPUGPU

- GPU,GPU 

#### 

- (iOS6BackBoard)


- **-**
	- **** - /
	- **** - -drawRect:-drawLayer:inContext:
	- **** - Core AnimationCore Animation
	- **** - Core AnimationIPC

- ,
	- OpenGL
	- 
- ****:,CPUGPUCore Animation.

#### GPU
- 
- CALayerGPU
- **GPU**
	- **** - iOSCore AnimationGPUIPCCPU
	- **** - GPUiOSGPUiPhone 3GS2.560
	- **** - CPUGPUGPUCore Animation
	- **** - GPU2048x20484096x4096CPU

#### CPU	
- 
	- **** - iOS6CPU
	- **** - iOSnibIOCPU
	- **Core Graphics** - -drawRect:CALayerDelegate-drawLayer:inContext:Core AnimationIPCCore Graphics
	- ****- PNGJPEG x  x 4iOSUIImageViewCore Graphics 

- CPUCore AnimationOpenGLGPUCore AnimationCPUCPUCPU

#### IO
- IO/URLnib
-   IOIO,

#### 
-  MacMacCPUiOSMacGPUiOSCPUGPUGPUCAEAGLLayerOpenGL

- 60FPSNSTimerCADisplayLink30FPSCore Animation60FPS

### Instruments:ProfileInstrumentsiOS
-  - /CPU
	- **** - 
	- **** - 
	- **Obj-C** - Objective-CCore AnimationCC++


- **Core Animation** - Core Animation
	- **Color Blended Layers** - GPU
	- **ColorHitsGreenandMissesRed**- shouldRasterizep
	- **Color Copied Images** - Core AnimationCPU
	- **Color Immediately**- Core Animation Instruments10
	- **Color Misaligned Images** - 
	-**Color Offscreen-Rendered Yellow** - shadowPathshouldRasterize
	- **Color OpenGL Fast Path Blue** - OpenGLUIKitCore AnimationAPIGLKViewCAEAGLLayerCPU
	- **Flash Updated Regions** - Core Graphicsbug

- OpenGL ES - GPUOpen GLCore Animation
	- Renderer Utilization - ~50%
	- Tiler Utilization - ~50% 

## 13.
- :Core AnimationGPU),****

#### :CAShapeLayerCore Graphics
- 
- 
- 
- 

```
- (void)awakeFromNib
{
    //create a mutable path
    self.path = [[UIBezierPath alloc] init];

    //configure the layer
    CAShapeLayer *shapeLayer = (CAShapeLayer *)self.layer;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineWidth = 5;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get the starting point
    CGPoint point = [[touches anyObject] locationInView:self];

    //move the path drawing cursor to the starting point
    [self.path moveToPoint:point];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get the current point
    CGPoint point = [[touches anyObject] locationInView:self];

    //add a new line segment to our path
    [self.path addLineToPoint:point];

    //update the layer with a copy of the path
    ((CAShapeLayer *)self.layer).path = self.path.CGPath;
}

```
#### :Mac OSiOS
-  -setNeedsDisplayInRect:-drawRect:-drawLayer:inContext:
-   -drawLayer:inContext:CGContextCGContextGetClipBoundingBox()-drawRect()CGRect
- **CGRectIntersectsRect()**

#### :CATiledLayerdrawsAsynchronously
- **CATiledLayer**-drawLayer:inContext:CATiledLayer,
	- CATiledLayer
	- CATiledLayerCGContext

- **drawsAsynchronously**:-drawLayer:inContext:CGContextCGContext-drawLayer:inContext:CGContext


## 14.IO

#### 
-  .
-  CPUIO/iOSRAM
-  200ms16ms20iOS23

- CPU
- :,
	- CPU:PNGJPEG(XcodePNG)
	- ImageIO:

	```
NSURL *imageURL = [NSURL fileURLWithPath:self.imagePaths[index]];
NSDictionary *options = @{(__bridge id)kCGImageSourceShouldCache: @YES}; 
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL);
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0,(__bridge CFDictionaryRef)options);
UIImage *image = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);
CFRelease(source);
	```

- 
	- CGContextiOS
	- CGContextiOS

#### 
-  - +
- +imageNamed::****
- 
	-  - 
	-  - 
	-  -  
	-  - NSCache

- NSCache:
	- -setCountLimit:
	- setObject:forKey:cost:

#### 
-  **PNGJPEG**
	- PNGJPEGPNG
	- iOSPNGJPEG
- ****:PNGRGBJPEG

	```
	//:PNGJPEG
	- (void)viewDidLoad
{
    [super viewDidLoad];
    //load color image
    UIImage *image = [UIImage imageNamed:@"Snowman.jpg"];
    //load mask image
    UIImage *mask = [UIImage imageNamed:@"SnowmanMask.png"];
    //convert mask to correct format
    CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray();
    CGImageRef maskRef = CGImageCreateCopyWithColorSpace(mask.CGImage, graySpace);
    CGColorSpaceRelease(graySpace);
    //combine images
    CGImageRef resultRef = CGImageCreateWithMask(image.CGImage, maskRef);
    UIImage *result = [UIImage imageWithCGImage:resultRef];
    CGImageRelease(resultRef);
    CGImageRelease(maskRef);
    //display result
    self.imageView.image = result;
}

	```
## 15.

#### 
- ****:CATextLayerUILabeliOS 6UILabelWebKitHTMLCATextLayerCore Text.
	- frame
	- 

- ****:
	- shouldRasterizecontents
	- **** 

#### :

- ()
	- cornerRadiusmaskToBounds

	- 

	- 

- CAShapeLayer:

```
CAShapeLayer *blueLayer = [CAShapeLayer layer];
    blueLayer.frame = CGRectMake(50, 50, 100, 100);
    blueLayer.fillColor = [UIColor blueColor].CGColor;
    blueLayer.path = [UIBezierPath bezierPathWithRoundedRect:
    CGRectMake(0, 0, 100, 100) cornerRadius:20].CGPath;
    
    //add it to our view
    [self.layerView.layer addSublayer:blueLayer];
```

- 

```
blueLayer.contentsScale = [UIScreen mainScreen].scale;
blueLayer.contents = (__bridge id)[UIImage imageNamed:@"Circle.png"].CGImage;

```
- shadowPath: Core Animation

#### :
- backgroundColor
- opaqueYES

#### 
-  IPCOpenGL
- :
- Core Graphics:UILabelUIImageView-drawRect:

- renderInContext
	-   CALayer-renderInContext:Core GraphicsUIImageViewcontentsshouldRasterize    +

-  shouldRasterizeCore Animation+



Web Proxy Viewer  |  New URL  |  Original Page