1

我最近问了一个类似的问题,关于如何为 UIView 添加半透明效果并得到了很好的回应。

然而,它使用了大量的 CPU 能力来处理,所以我使用了答案背后的一些想法,但使用 GPUImage 进行过滤,效率更高。

它适用于静态屏幕,但我想UIImageView用动画更改背景的图像。但是,当我设置UIView在过渡期间对背景进行采样时,它似乎会忽略过渡并在动画开始之前显示新图像。
相关代码如下(如果需要更多请询问!):

包含自定义UIViewUIImageView背景的超级视图:

//The Transition
[contentView setScheduled:YES]; //Starts the sampling every 0.2 seconds

//bg is the UIImageView and contentView is the 'translucent' UIView subclass
[UIView transitionWithView:bg duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    [bg setImage:newImage];
}completion:^(BOOL finished){
    [contentView setScheduled:NO];
}];

然后在 UIView 子类中:

- (UIImage *)snapshotOfSuperview:(UIView *)superview
{
CGFloat scale = 0.5;
if (([UIScreen mainScreen].scale > 1 || self.contentMode == UIViewContentModeScaleAspectFill))
{
    CGFloat blockSize = 12.0f/5;
    scale = blockSize/MAX(blockSize * 2, floor(self.blurRadius));
}
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -self.frame.origin.x, -self.frame.origin.y);
self.hidden=YES; //Don't take a snapshot of the view
[superview.layer renderInContext:context];
self.hidden=NO;
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshot;
}

-(void)updateViewBG{
UIImage *superviewImage = [self snapshotOfSuperview:self.superview];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    GPUImageGaussianBlurFilter* filter = [[GPUImageGaussianBlurFilter alloc] init];
    filter.blurSize = 0.8f;
    UIImage* newBG = [self applyTint:self.tintColour image:[filter imageByFilteringImage:superviewImage]];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.layer.contents = (id)newBG.CGImage;
        self.layer.contentsScale = newBG.scale;
    });
});
}

-(UIImage*)applyTint:(UIColor*)colour image:(UIImage*)inImage{
UIImage *newImage;
if (colour) {
    UIGraphicsBeginImageContext(inImage.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, inImage.size.width, inImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, inImage.CGImage);
    [colour set];
    CGContextFillRect(ctx, area);
    CGContextRestoreGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeLighten);
    CGContextDrawImage(ctx, area, inImage.CGImage);
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}else{
    newImage = inImage;
}
return newImage;
}

-(void)displayLayer:(CALayer *)layer{
[self updateViewBG];
}

如何让背景跟随动画?

4

1 回答 1

2

我认为问题在于,一旦您更改图像,就会调用 displayLayer(忽略正在进行的转换),因此新图像用于半透明。

您应该进行修改updateViewBG,使其在过渡完成之前不会更新图层内容。例如,您可以为您的班级添加一个标志,在您开始转换时设置它并在它完成时重置它。设置标志时,您不要调用updateViewBG.

[UIView transitionWithView:bg duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{

    self.isTransitionInProgress = YES;
    [bg setImage:newImage];

}completion:^(BOOL finished){

    [contentView setScheduled:NO];

    self.isTransitionInProgress = NO;
    [contentView.layer setNeedsDisplay];

}];



-(void)displayLayer:(CALayer *)layer{
    if (!self.isTransitionInProgress)
        [self updateViewBG];
}
于 2013-10-04T18:54:47.547 回答