漂亮的下落式动画载入视图

- (IBAction)changePassWord:(id)sender

{

    CGSize size = [[UIScreen mainScreen] bounds].size;

    

    //增加半透明层

    bgView = [[UIView alloc]initWithFrame:self.view.frame];

    bgView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.7];

    [self.parentViewController.view addSubview:bgView];

    

    //把修改密码窗口加到父视图上,因为UITableView没法直接加subview。给窗口一个初始化的旋转角度。

    self.changePWD = [[self storyboard]instantiateViewControllerWithIdentifier:@"ChangePassWordID"];

    self.changePWD.delegate = self;

    [self.changePWD.view setFrame:CGRectMake(0, -200, 280, 200)];

    [self.changePWD.view setTransform:CGAffineTransformMakeRotation(-M_PI / 20)];

    [self.parentViewController.view addSubview:self.changePWD.view];

    

    //动画载入密码窗口

    [UIView animateWithDuration:0.3 animations:^{

        [self.changePWD.view setTransform:CGAffineTransformMakeRotation(0)];

        [self.changePWD.view setFrame:CGRectMake(20, size.height/2-100, 280, 200)];

    }];

    

    //给窗口增加拖动手势

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPaswordView:)];

    [panRecognizer setMinimumNumberOfTouches:1];

    [panRecognizer setMaximumNumberOfTouches:1];

    [self.parentViewController.view addGestureRecognizer:panRecognizer];

    

}

 

-(void)panPaswordView:(id)sender {

    [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    

    //拖动开始时记录浮动窗口的中心点

    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = self.changePWD.view.center.x;

        firstY = self.changePWD.view.center.y;

    }

    

    //获取拖动后的坐标

    translatedPoint = CGPointMake(firstX, firstY+translatedPoint.y);

    

    //禁止往上拖动

    if (translatedPoint.y < firstY) {

        return;

    }

    

    //在拖动时即时更新窗口位置

    [self.changePWD.view setCenter:translatedPoint];

    

    //拖动结束时关闭密码窗口,并移除附加的手势

    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        

        [self removePasswordView];

        

        [self.parentViewController.view removeGestureRecognizer:(UIPanGestureRecognizer*)sender];

    }

}

 

- (void)removePasswordView

{

    [UIView animateWithDuration:0.5 animations:^{

        

        [self.changePWD.view setFrame:CGRectMake(20, 1136, 280, 200)];

        bgView.alpha = 0.0;

        

    } completion:^(BOOL finished) {

        [self.changePWD.view removeFromSuperview];

        [bgView removeFromSuperview];

    }];

}

你可能感兴趣的:(视图)