Get UITableviewCell position from “visible” area or window

前端 未结 3 1674
[愿得一人]
[愿得一人] 2020-12-28 15:30

I create an iPhone app base on uitableview and uitabbar. On each cell of tableview i have an \"add to favorite button\". When i pressed this button, i want to make the cell

3条回答
  •  盖世英雄少女心
    2020-12-28 15:55

    So here is the code i used : (it can be optimized i'm sure) :

    First add NSindexPath ** property to your custom cell.
    *Implement this in your tableview controller : *

    -(void)addCellToFavorisWithAnimation:(ResultSearchOffreCell*)cell{
    /* Generate image from cell
    ----------------------------------------------------------*/
    UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, [[UIScreen mainScreen] scale]);
    [cell.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // Launch first animation (go to top-right)
    //----------------------------------------------------------*/
    CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:cell.indexPath];
    CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
    
    cellAnimationContainer = [[UIImageView alloc] initWithFrame:rectInSuperview];
    
    [cellAnimationContainer setFrame:CGRectMake(cellAnimationContainer.frame.origin.x, cellAnimationContainer.frame.origin.y+50 , cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)];
    
    [cellAnimationContainer setImage:img];
    
    AppDelegate_Shared *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:cellAnimationContainer];
    [UIView beginAnimations:@"addFavorite-Step1" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [cellAnimationContainer setFrame:CGRectMake(20,cellAnimationContainer.frame.origin.y-10, cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)];
    [cellAnimationContainer setAlpha:0.7];
    [UIView commitAnimations];
    }
    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    
    /* Launch second animation => go to favoris Tab and remove from self.view
     ---------------------------------------------------------------------------*/
    if([anim isEqual:@"addFavorite-Step1"])
    {
        [UIView beginAnimations:@"addFavorite-Step2" context:nil]; 
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationDelegate:self]; 
        [cellAnimationContainer setAlpha:0.4];
        [cellAnimationContainer setFrame:CGRectMake(150, 420, 20, 20)];
        [UIView commitAnimations];
    }
    else if([anim isEqual:@"addFavorite-Step2"])
    {
        [cellAnimationContainer removeFromSuperview];
        [cellAnimationContainer release];         
    }
    }
    

提交回复
热议问题