我有两张图片。两个相同的场景,相同的大小,占据了整个屏幕。一个是图像模糊,一个是焦点。期望的效果是用户最初会看到模糊的图像,当他们在屏幕上从左到右拖动手指时,他们将看到图像左侧的部分,他们拖动的位置是焦点(例如,如果他们拖动只有一半,然后场景的左半部分是聚焦的图像,而右半部分仍然是模糊的。
我通过UIImageView
将聚焦图像作为图像进行子类化来做到这一点 - 然后添加CALayer
应用蒙版的模糊图像,然后根据 touchesBegan/touchesMoved 更改蒙版的位置。问题是使用下面的方法性能非常慢。所以我想知道我做错了什么。
@interface DragMaskImageView : UIImageView
@end
@implementation DragMaskImageView{
BOOL userIsTouchingMask;
CALayer *maskingLayer;
CALayer *topLayer;
float horzDistanceOfTouchFromCenter;
}
- (void)awakeFromNib {
topLayer = [CALayer layer];
topLayer.contents = (id) [UIImage imageNamed:@"blurryImage.jpg"].CGImage;
topLayer.frame = CGRectMake(0, 0, 480, 300);
[self.layer addSublayer:topLayer];
maskingLayer = [CALayer layer];
maskingLayer.contents = (id) [UIImage imageNamed:@"maskImage.png"].CGImage;
maskingLayer.anchorPoint = CGPointMake(0.0, 0.0);
maskingLayer.bounds = CGRectMake(0, 0, 480, 300);
[topLayer setMask:maskingLayer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[[event allTouches] anyObject] locationInView:self];
if (touchPoint.x < maskingLayer.frame.origin.x) {
NSLog(@"user is touching to the left of mask - disregard");
userIsTouchingMask = NO;
} else {
NSLog(@"user is touching ");
horzDistanceOfTouchFromCenter = touchPoint.x - maskingLayer.frame.origin.x;
userIsTouchingMask = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (userIsTouchingMask) {
CGPoint touchPoint = [[[event allTouches] anyObject] locationInView:self];
float newMaskX = touchPoint.x - horzDistanceOfTouchFromCenter;
if (newMaskX < 0) {
newMaskX = 0;
}
if (newMaskX > 480) {
newMaskX = 480;
}
maskingLayer.frame = CGRectMake(newMaskX, 0 ,480, 300);
}
}
我检查了相关的线程核心动画 calayer 蒙版动画性能,但在任何层上设置shouldRasterize
为 YES 似乎对性能问题没有帮助。