开发者

Blending a UITableViewCell with a background

开发者 https://www.devze.com 2023-03-19 20:15 出处:网络
I have a dilemma I can\'t seem to solve. I have a UITableView populated by custom UITableViewCells and I need the background of each cell to be blended (like Photoshop multiply) with the background im

I have a dilemma I can't seem to solve. I have a UITableView populated by custom UITableViewCells and I need the background of each cell to be blended (like Photoshop multiply) with the background image (the background image is part of the UIView behind the UITableView).

I've found several ways to do the blend (and it does somewhat work). The blending function I'm using is:

 - (UIImage *)blendOverlay:(UIImage *)topImage withBaseImage:(UIImage *)baseImage toSize:(CGFloat)imageSize
{
    UIGraphicsBeginImageContext(CGSizeMake(imageSize, imageSize));
    [baseImage drawInRect:CGRectMake(0.0, 0.0, imageSize, imageSize)];
    [topImage drawInRect:CGRectMake(0.0, 0.0, imageSize, imageSize) blendMode:kCGBlendModeMulitply alpha:0.5];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

which is from http://www.cocoabuilder.com/archive/cocoa/280142-uiimageview-display-as-multiply.html.

Basically what I do now is chop out the piece of the background I'm over by getting the cell's frame and creating a new image which is the piece of the background in that frame. However, in order for the effect to work, it essentially needs to recalculate and update every frame (much like an alpha effect). Oh, and I tried just setting a timer to 1/60 seconds and calling

[tableView reloadData];

But that just slowed everything down and the redraws never actually occurred. Any help is much appreciated, I've been googling for nearly 3 hours to avail.

Oh, and for posterity purposes, I chop the background image like so:

CGRect r = [tableView rectForRowAtIndexPath:indexPath];
CGRect r2 = [tableView convertRect:r toView:self.view];

CGImageRef imageRef = CGImageCreateWithImageInRect([m_background.image CGImage], r2);
// Get th开发者_运维问答e image by calling [UIImage imageWithCGImage:imageRef]
CGImageRelease(imageRef);

Thanks!


The problem is that a UITableView doesn't call the drawRect: method of the each individual UITabelViewCell while moving the rows. It seems to cache the view at the start of the table movement, and use that image for moving the whole table. Therefore the custom UITableViewCell has no opportunity to update the background image to show the movement of the blended backgrounds.

The one possible way I can see to animate the background image blending as the table cells move is to subclass UITableView and override drawRect: in that subclass.

0

精彩评论

暂无评论...
验证码 换一张
取 消