0

我需要实现一个动画,该动画在从滚动视图中删除对象时起作用。

例如,滚动视图中有 5 个对象(视图)。当一个对象被删除时,另一个将与动画一起转到另一个旁边。也就是说,当第三个被删除时,第四个和第五个将转到第二个,它看起来像是包含四个对象的滚动视图。

我怎样才能达到这个要求?

任何想法?

self.frame = CGRectMake(0, 0, 1024, 768);



    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    deleteButton.frame = CGRectMake(0, 0, 100, 100);
    [deleteButton setTitle:@"fuck" forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(removeViews) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:deleteButton];




    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 200, 800, 100)];
    [scrollView setScrollEnabled:YES];
    scrollView.pagingEnabled = YES;
    [scrollView setDelegate:self];
    scrollView.backgroundColor = [UIColor brownColor];
    [scrollView setShowsHorizontalScrollIndicator:YES];
    [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.contentMode = UIViewContentModeScaleToFill;
    [scrollView setContentSize:CGSizeMake(1000,100)];


    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];


    int countViews = 6;

    //for each number add one circleview to your scrollview. for each circleview add a button, to remove the view from the scrollview
    for(int i=0;i<countViews;i++){




        UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(100+(i*100),5,90,90)];
        circleView.backgroundColor = [UIColor whiteColor];
        circleView.tag = i;
        [scrollView addSubview:circleView];

        UILabel* circleIndex = [[UILabel alloc] init];
        circleIndex.frame    = CGRectMake(30, 25, 40, 40);
        [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
        [circleIndex setText:[NSString stringWithFormat:@"%d",i]];

        [circleView addSubview:circleIndex];                

        UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)];
        exitView.layer.cornerRadius = 15;
        exitView.backgroundColor = [UIColor redColor];
        exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
        exitView.layer.borderWidth = 2;

        exitView.tag = i;


        [exitView setHidden:YES];
        [circleView addSubview:exitView];




        UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)];
        exitLabel.backgroundColor = [UIColor clearColor];
        exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
        exitLabel.layer.borderWidth = 2;

        [exitView addSubview:exitLabel];


        UILongPressGestureRecognizer *longPress =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                      action:@selector(handleLongPress:)];
        [circleView addGestureRecognizer:longPress];

        UITapGestureRecognizer *singlePress =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handleSinglePress:)];
        [exitView addGestureRecognizer:singlePress];

    }




//[circleView bringSubviewToFront:exitView];


    [self addSubview:scrollView];



}
return self;
}


-(void)removeViews:(id)sender{


[[scrollView viewWithTag:[sender tag]] removeFromSuperview];
[[scrollView viewWithTag:([sender tag]-100)] removeFromSuperview];

//[scrollView setNeedsDisplay];

//[self setNeedsDisplay];

NSLog(@"%d",scrollView.subviews.count);


for(int i=0; i<(scrollView.subviews.count/2-[sender tag]+1);i++)
{

[UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     [[scrollView viewWithTag:([sender tag]+i)] setFrame:CGRectMake(100+(i*100),5,90,90)];   // last position
                 }
                completion:nil];

 }


      }  
 }


//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {

    [self shakeView:recognizer.view];
    [[recognizer.view viewWithTag:2] setHidden:NO];
 }
}

- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {

    [self removeViews:[recognizer.view viewWithTag:4]];

 }
}

- (void)singlePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {

    [recognizer.view.layer removeAllAnimations];

    //[exitView setHidden:YES];
 }
}
4

1 回答 1

0

删除对象后,您需要调用[scrollview setneedsDisplay]刷新滚动视图

于 2013-07-24T08:32:41.463 回答