Skip to content

Commit a805380

Browse files
Merge pull request #8 from edusperoni/ios-performance
Better iOS Performance with shadowPath and rasterize
2 parents 987749c + 15103d6 commit a805380

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ The table below list and describes all possible attributes as well as show which
9797
| shadowOffset | number | iOS | Determines the distance in points (only on Y axis) of the shadow. Negative value shows the shadow above the view. |
9898
| shadowOpacity | number | iOS | From 0 to 1. Determines the opacity level of the shadow. |
9999
| shadowRadius | number | iOS | Determines the blurring effect in points of the shadow. The higher the more blurred. |
100+
| useShadowPath | boolean => default: true | iOS | Determines whether to use shadowPath to render the shadow. Setting this to false negatively impacts performance. |
101+
| rasterize | boolean => default: false | iOS | Determines whether the view should be rasterized. Activating this will increase memory usage, as rasterized views are stored in memory, but will massively improve performance. |
100102

101103
### `AndroidData` and `IOSData`
102104

lib/src/nativescript-ngx-shadow/common/ios-data.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ export class IOSData {
55
shadowOffset?: number;
66
shadowOpacity?: number;
77
shadowRadius?: number;
8+
rasterize?: boolean;
9+
useShadowPath?: boolean;
810
}

lib/src/nativescript-ngx-shadow/common/shadow.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { AndroidData } from "./android-data.model";
44
import { IOSData } from "./ios-data.model";
55
import { ShapeEnum } from './shape.enum';
66
import { Length } from 'tns-core-modules/ui/page/page';
7-
import { isAndroid } from "tns-core-modules/platform";
7+
import { isAndroid, screen } from "tns-core-modules/platform";
88

99
declare const android: any;
1010
declare const java: any;
1111
declare const CGSizeMake: any;
1212
declare const UIScreen: any;
1313
declare const Array: any;
14+
declare const UIBezierPath: any;
1415

1516
let LayeredShadow;
1617
let PlainShadow;
@@ -49,6 +50,8 @@ export class Shadow {
4950
pressedTranslationZ: (data as AndroidData).pressedTranslationZ || Shadow.DEFAULT_PRESSED_ELEVATION,
5051
shadowColor: (data as IOSData).shadowColor ||
5152
Shadow.DEFAULT_SHADOW_COLOR,
53+
useShadowPath: ((data as IOSData).useShadowPath !== undefined ? (data as IOSData).useShadowPath : true),
54+
rasterize: ((data as IOSData).rasterize !== undefined ? (data as IOSData).rasterize : false)
5255
},
5356
);
5457
}
@@ -191,6 +194,13 @@ export class Shadow {
191194
data.shadowRadius ?
192195
parseFloat(String(data.shadowRadius)) :
193196
0.66 * elevation - 0.5;
197+
nativeView.layer.shouldRasterize = data.rasterize;
198+
nativeView.layer.rasterizationScale = screen.mainScreen.scale;
199+
let shadowPath = null;
200+
if(data.useShadowPath) {
201+
shadowPath = UIBezierPath.bezierPathWithRoundedRectCornerRadius(nativeView.bounds, nativeView.layer.shadowRadius).cgPath;
202+
}
203+
nativeView.layer.shadowPath = shadowPath;
194204
}
195205

196206
static androidDipToPx(nativeView: any, dip: number) {

lib/src/nativescript-ngx-shadow/ng-shadow.directive.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export class NativeShadowDirective implements OnInit, OnChanges, AfterViewInit {
3535
@Input() shadowOffset?: number | string;
3636
@Input() shadowOpacity?: number | string;
3737
@Input() shadowRadius?: number | string;
38+
@Input() useShadowPath?: boolean;
39+
@Input() rasterize?: boolean;
3840

3941
private loaded = false;
4042
private initialized = false;
@@ -131,7 +133,9 @@ export class NativeShadowDirective implements OnInit, OnChanges, AfterViewInit {
131133
changes.hasOwnProperty('shadowColor') ||
132134
changes.hasOwnProperty('shadowOffset') ||
133135
changes.hasOwnProperty('shadowOpacity') ||
134-
changes.hasOwnProperty('shadowRadius'))
136+
changes.hasOwnProperty('shadowRadius') ||
137+
changes.hasOwnProperty('rasterize') ||
138+
changes.hasOwnProperty('useShadowMap'))
135139
) {
136140
if (
137141
changes.hasOwnProperty('shadow') &&
@@ -191,7 +195,9 @@ export class NativeShadowDirective implements OnInit, OnChanges, AfterViewInit {
191195
shadowColor: this.shadowColor,
192196
shadowOffset: this.shadowOffset as number,
193197
shadowOpacity: this.shadowOpacity as number,
194-
shadowRadius: this.shadowRadius as number
198+
shadowRadius: this.shadowRadius as number,
199+
rasterize: this.rasterize,
200+
useShadowPath: this.useShadowPath
195201
});
196202
}
197203
}
@@ -244,5 +250,7 @@ export class NativeShadowDirective implements OnInit, OnChanges, AfterViewInit {
244250
this.shadowOffset = data.shadowOffset || this.shadowOffset;
245251
this.shadowOpacity = data.shadowOpacity || this.shadowOpacity;
246252
this.shadowRadius = data.shadowRadius || this.shadowRadius;
253+
this.rasterize = data.rasterize || this.rasterize;
254+
this.useShadowPath = data.useShadowPath || this.useShadowPath;
247255
}
248256
}

0 commit comments

Comments
 (0)