2011-11-21 75 views
0

我创建了UITabBar模板。然后我添加到firstViewController一个tableView。建立连接(UITableViewDataSource和UITableViewDelegate)。 在didselect方法中,我需要呈现选定的UITableViewCell并将其动画到UITabBars之一。 如果我渲染UITableViewCell row = 0 - 没关系,但是如果我渲染UITableViewCell row> 0(1,2,...),我会得到黑色的矩形。 我可以呈现UITableViewCell contentView,但它会呈现无背景。 如何获得具有背景的UITableViewCell的快照?如何渲染UITableViewCell

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) UITabBarController *tabBarController; 

@end 

#import "AppDelegate.h" 
#import "FirstViewController.h" 
#import "SecondViewController.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize tabBarController = _tabBarController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 


#import <UIKit/UIKit.h> 

@interface FirstViewController : UIViewController <UITableViewDelegate,  UITableViewDataSource> 

@property (nonatomic, strong) IBOutlet UITableView *tableViewAnimation; 
@property (nonatomic, retain) UIImageView *thumbnail; 

- (UIImage*)screenshotFromView:(UIView *)view; 

@end 


#import "FirstViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation FirstViewController 
@synthesize tableViewAnimation = _tableViewAnimation; 
@synthesize thumbnail = _thumbnail; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"First", @"First"); 
     self.tabBarItem.image = [UIImage imageNamed:@"first"]; 
    } 
    return self; 
} 


#pragma mark - UITableView DataSource 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 
    NSUInteger count = 10; 
    return count; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    static NSString *goodsListCellIdentifier = @"ListOfGoodsIndetifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsListCellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsListCellIdentifier]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.textLabel.text = @"detalied view"; 

return cell; 
} 

#pragma mark - UITableView Delegate 
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
cell.backgroundColor = [UIColor redColor]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [_tableViewAnimation cellForRowAtIndexPath:indexPath]; 
    CGRect imageViewFrame = cell.frame; 
    imageViewFrame.origin.y = 64.0 + tableView.frame.origin.y + imageViewFrame.origin.y  - tableView.contentOffset.y; 
// imageViewFrame.size.width = cell.contentView.frame.size.width; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame]; 

    imageView.image = [self screenshotFromView:cell]; 
    [self.tabBarController.view addSubview:imageView]; 
    self.thumbnail = imageView; 
    self.thumbnail.hidden = NO; 
    self.view.userInteractionEnabled = NO; 
    [UIView animateWithDuration:1.0 
        animations:^{ 
         CGRect tabBarFrame = self.tabBarController.tabBar.frame; 
         CGFloat xoffset = tabBarFrame.size.width/2; 
         [_thumbnail setCenter:CGPointMake(xoffset * 0.5, tabBarFrame.origin.y + 25.0)]; 
         [_thumbnail setTransform:CGAffineTransformMakeScale(0.1, 0.1)]; 
         [_thumbnail setAlpha:0.4]; 
        } 
        completion:^(BOOL finished){ 
         [self.thumbnail removeFromSuperview]; 
         self.thumbnail = nil; 
         self.view.userInteractionEnabled = YES; 
        }]; 
} 

- (UIImage*)screenshotFromView:(UIView *)view { 
    // Create a graphics context with the target size 
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration 
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext 
    CGSize imageSize = view.bounds.size; 
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // -renderInContext: renders in the coordinate space of the layer, 
    // so we must first apply the layer's geometry to the graphics context 
    CGContextSaveGState(context); 
    // Center the context around the window's anchor point 
    CGContextTranslateCTM(context, [view center].x, [view center].y); 
    // Apply the window's transform about the anchor point 
    CGContextConcatCTM(context, [view transform]); 
    // Offset by the portion of the bounds left of and above the anchor point 
    CGContextTranslateCTM(context, -[view bounds].size.width * [[view layer] anchorPoint].x, -[view bounds].size.height * [[view layer] anchorPoint].y); 

    // Render the layer hierarchy to the current context 
    [[view layer] renderInContext:context]; 

    // Restore the context 
    CGContextRestoreGState(context); 

    // Retrieve the screenshot image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return image; 
} 

@end 
+0

你说的渲染'UITableViewCell'是什么意思? – Legolas

+0

从UITableViewCell或快照创建图像 – rowwingman

回答

0

只是注释下一行:

// Center the context around the window's anchor point 
// CGContextTranslateCTM(context, [view center].x, [view center].y); 
// Apply the window's transform about the anchor point 
// CGContextConcatCTM(context, [view transform]); 
// Offset by the portion of the bounds left of and above the anchor point 
// CGContextTranslateCTM(context, -[view bounds].size.width * [[view layer] anchorPoint].x, -[view bounds].size.height * [[view layer] anchorPoint].y); 
0

背景可能不是由单元格自动呈现,而是由表格代替。 请注意,根据单元格状态(背景/选定背景)呈现背景。

您可以在渲染单元格之前取出cell.backgroundView并渲染它。 [((UITableViewCell*) view).backgroundView.layer renderInContext:context]

但这只是一个猜测。

也许这是更简单。当你点击一个单元格时,开始选择动画。 renderInContext忽略动画。也许有问题?

+0

以及如何编写backgroundView快照和contentView快照?为什么对于UITableViewCell row = 0没关系,但对于其他的它是黑色的? – rowwingman