2011-11-21 3 views
0

Je crée un modèle UITabBar. Ensuite, j'ajoute à firstViewController une tableView. Établissez des connexions (UITableViewDataSource et UITableViewDelegate). Dans la méthode didselect, j'ai besoin de rendre le fichier UITableViewCell sélectionné et de l'animer dans l'un des UITabBars. Si je rend UITableViewCell row = 0 - c'est ok, mais si je rend UITableViewCell row> 0 (1,2, ...) je reçois un rectangle noir. Je peux rendre UITableViewCell contentView, mais il sera rendu sans arrière-plan. Comment obtenir un instantané de UITableViewCell avec un arrière-plan?Comment faire pour rendre 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

Que voulez-vous dire en rendant 'UITableViewCell'? – Legolas

+0

créer une image à partir de UITableViewCell ou de l'instantané – rowwingman

Répondre

0

Tout commentaire suivant: lignes

// 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

L'arrière-plan n'est probablement pas rendu automatiquement par la cellule mais par la table à la place. Notez que l'arrière-plan est rendu en fonction de l'état de la cellule (arrière-plan/arrière-plan sélectionné).

Vous pouvez prendre cell.backgroundView et le rendre avant de rendre la cellule. [((UITableViewCell*) view).backgroundView.layer renderInContext:context]

Mais ce n'est qu'une supposition.

Peut-être que c'est quelque chose de plus simple. Lorsque vous appuyez sur une cellule, l'animation de sélection est démarrée. renderInContext ignore les animations. Peut-être qu'il y a un problème?

+0

Et comment composer snapshot backgroundView et snapshot contentView? Et pourquoi pour UITableViewCell row = 0 c'est ok, mais pour les autres c'est noir? – rowwingman

Questions connexes