how to make smooth UIView splitting animation?

2019-08-24 18:51发布

问题:

Hi i am trying to implement split the UIView using UIPinchGestureRecognizer, but the following issue are their, if any one know how to solve kindly.

  1. This is my pinchGestureHandlerCode:

    - (IBAction)splitView:(UIPinchGestureRecognizer*)recognizer
    {
        if(recognizer.state == UIGestureRecognizerStateBegan)
        {
            CGPoint selectedFolderPoint = CGPointMake([recognizer locationInView:recognizer.view].x,
                                              recognizer.view.frame.origin.y);
            self.splitPosition = ([recognizer locationOfTouch:0 inView:recognizer.view].x +
                          [recognizer locationOfTouch:1 inView:recognizer.view].x)/2;
    
    
        //STEP 1: Capture the Main View Content into an image
        [self captureImageFromPointAndSetupMaskView:selectedFolderPoint];
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged)
    {
         CGPoint selectedFolderPoint = CGPointMake(recognizer.scale,
                                              recognizer.view.frame.origin.y);
    
        //STEP 2: Reduce the left image width, and move the right image origin according to the finger move of user.
        [self layoutBottomPartOfMainViewRelativeToPointInMainView:selectedFolderPoint];
    
    
        [UIView beginAnimations:@"split" context:NULL];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        //captures the main background view's image
    
        //STEP 3- Push The Layer-3 which hosts the other part of the Main Content View
        [UIView commitAnimations];
    
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        // STEP 3: remove the both image view from the view.
        [self layoutFinalFrameOfBottomPartOfMainContentView];
        [UIView commitAnimations];
    }
    

    }

STEMP 1 METHOD:

-(void)captureImageFromPointAndSetupMaskView:(CGPoint)selectedFolderPoint
{
    UIGraphicsBeginImageContext(self.superview.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

    CGImageRef leftTempRef = backgroundImage.CGImage;
    CGImageRef leftImageRef = CGImageCreateWithImageInRect(leftTempRef, 

CGRectMake(self.bounds.origin.x, self.bounds.origin.y, selectedFolderPoint.x, self.bounds.size.height));

    UIImage *leftImage = [UIImage imageWithCGImage:leftImageRef];

    leftImageBackgroundView = [[UIImageView alloc] initWithImage:leftImage];
    [self.leftBackgroundView addSubview:leftImageBackgroundView];

    CGImageRef rightTempRef = backgroundImage.CGImage;
    CGImageRef rightImageRef = CGImageCreateWithImageInRect(rightTempRef, 

CGRectMake(selectedFolderPoint.x, self.bounds.origin.y, self.superview.bounds.size.width, self.bounds.size.height));

    UIImage *rightImage = [UIImage imageWithCGImage:rightImageRef];

    rightImageBackgroundView = [[UIImageView alloc] initWithImage:rightImage];
    [self.rightBackgroundView addSubview:rightImageBackgroundView];



    [self.leftBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
                                             self.bounds.origin.y,
                                             selectedFolderPoint.x,
                                             self.frame.size.height)];

    [self.rightBackgroundView setFrame:CGRectMake(selectedFolderPoint.x,
                                              self.bounds.origin.y,
                                              self.superview.bounds.size.width,
                                              self.bounds.size.height)];
}

STEP 2 METHOD:

-(void)layoutBottomPartOfMainViewRelativeToPointInMainView:(CGPoint)selectedFolderPoint
{
[self.leftBackgroundView setFrame:CGRectMake(self.leftBackgroundView.bounds.origin.x,
                                             self.leftBackgroundView.bounds.origin.y,
                                             (self.leftBackgroundView.bounds.size.width -
                                                selectedFolderPoint.x),
                                             self.leftBackgroundView.bounds.size.height)];

[self.rightBackgroundView setFrame:CGRectMake(self.rightBackgroundView.frame.origin.x + selectedFolderPoint.x,
                                              self.rightBackgroundView.bounds.origin.y,
                                              self.rightBackgroundView.bounds.size.width - selectedFolderPoint.x,
                                              self.rightBackgroundView.bounds.size.height)];

if(((ABS(self.leftBackgroundView.bounds.origin.x+self.leftBackgroundView.bounds.size.width)-
     self.rightBackgroundView.bounds.origin.x) > 100) &&
   ((ABS(self.leftBackgroundView.bounds.origin.x+self.leftBackgroundView.bounds.size.width)-
     self.rightBackgroundView.bounds.origin.x) < 110))
{
    [self switchView];
    NSDictionary *message;
    [self.timelineController sendEvent:SWITCH_VIEW withMessage:message];
    [self layoutFinalFrameOfBottomPartOfMainContentView];

}
[UIView setAnimationsEnabled:NO];
[UIView setAnimationsEnabled:YES];
}

STEP 3 METHOD:

-(void)layoutFinalFrameOfBottomPartOfMainContentView
 {
     [self.leftBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
                                             self.leftBackgroundView.bounds.origin.y,
                                             self.bounds.origin.x,
                                               self.leftBackgroundView.bounds.size.height)];

     [self.rightBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
                                              self.rightBackgroundView.bounds.origin.y,
                                              self.bounds.origin.x,
                                              self.rightBackgroundView.bounds.size.height)];

    leftImageBackgroundView.image = nil;
    rightImageBackgroundView.image = nil;
}

The issue are:

  1. i am trying to make smooth animation but its running too fast and suddenly moving both side.
  2. if i to same pinch gesture more number of time app getting crash and showing memory warning.

if any one know what mistake i am doing in my code kindly help me to solve the problem or there is any other way to do the same animation kindly give way to me....

Thanks in advance... ...

回答1:

Define the UIPinchGestureRecognizer like this

UIPinchGestureRecognizer *ges = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(split)];
[self addGestureRecognizer:ges];

and the split Method like this

- (void)split {
    CGRect f = self.frame;
    CGRect f1 = CGRectMake(CGRectGetMinX(f), f.origin.y, f.size.width/2, f.size.height);
    CGRect f2 = CGRectMake(CGRectGetMidX(f), f.origin.y, f.size.width/2, f.size.height);

    SplitView *view1 = [[[SplitView alloc] initWithFrame:f1] autorelease];
    [self.superview addSubview:view1];

    SplitView *view2 = [[[SplitView alloc] initWithFrame:f2] autorelease];
    [self.superview addSubview:view2];

    f1.origin.x -= 30;
    f2.origin.x += 30;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    view1.frame = f1;
    view2.frame = f2;
    [UIView commitAnimations];

    [self removeFromSuperview];
}