简体   繁体   中英

UIScrollView remove subview animated

What I have: Basically I have collection of UIViews presented in horizontal scrollView. UIScrollView have paging enabled and each item is not full screen so I can see part of previous and next view based on this. Each view have delete button on itself.

What I need: I need to find good idea to animate reloading UIScrollView content after removing one of the subview. As I thought about it there will be 4 different kinds of deletion: 1. deletion of last view 2. deletion of first view 3. deletion of middle view - hardest one 4. deletion of only view - easiest one, just animation of view disappearing

I need some suggestions/solutions. Thanks, Michal

edit:

Ok, I havent implemented it fully.

Here is reload method:

- (void)loadScrollView{
for(UIView* view in [_scrollView subviews])
{
    [view removeFromSuperview];
}

CGRect frame = self.scrollView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
int i = 0;

_scrollView.clipsToBounds = NO;
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;

for(MPContact* contact in self.items)
{
    frame.origin.x = self.scrollView.frame.size.width*i;
    MyView *view = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
    [view setFrame:frame];

    i++;
    [view.nameAndSurnameLabel setText:@"view text"];
    [view setDelegate:self];
    [self.scrollView addSubview:view];[self.viewsArray addObject:view];
}[self.scrollView setContentSize:CGSizeMake(frame.size.width*[self.items count], frame.size.height)];}

Each MyView have delete button with delegate, delegate removes view from viewsArray and run this method for now.

I suggest you to use + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion method of UIView class. Just get your UIView that you want to delete, from UIScrollView and set view.alpha = 0 in animations block. In completion block remove this UIView from UIScrollView , using removeFromSuperView method. Setting alpha to 0 will make fade animation.

EDIT: Iterate all your views you want to move and change it's x coordinate.

for(UIView *view in scrollview.subviews)
{
    [UIView animateWithDuration:0.1 animations:^{
       [view setFrame:CGRectMake(view.x-(/*HERE YOU NEED TO PUT WIDTH OF TOUR DELETING VIEW*/),view.y,view.width,view.heigh)];
   }
    completion:^(BOOL finished){
    //remove view from superview and change your uiscrollview's content offset.
   }];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM