在平移手势或拖动上更改视图Alpha

1 objective-c uiview ios

我想将UIView拖动到Pan Gesture上的屏幕底部,但是当它到达屏幕底部时,视图alpha应该缩小到“零”。

反之亦然,当我将视图向上拖动时,UIView alpha应该缩小为“ 1”

但是问题在于,在平移屏幕的一半时或有时在我拖慢视图时,视图的Alpha值会缩小为“零”
最初,我已将UIView背景色设置为黑色。

我需要逐渐缩小视图的Alpha值,任何想法或建议都会有所帮助。

  UIPanGestureRecognizer * panner = nil;
    panner = [[UIPanGestureRecognizer alloc] initWithTarget: self action:@selector(handlePanGesture:)];
    [self.view addGestureRecognizer:panner ];
    [panner setDelegate:self];
    [panner release];


    CGRect frame = CGRectMake(0, 0, 320, 460);
    self.dimmer = [[UIView alloc] initWithFrame:frame];
    [self.dimmer setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:dimmer];



     -(IBAction) handlePanGesture:(UIPanGestureRecognizer *) sender {

      static CGPoint lastPosition = {0}; 
      CGPoint nowPosition; float alpha = 0.0;
      float new_alpha = 0.0;

        nowPosition = [sender translationInView: [self view]];
         alpha = [dimmer alpha] -0.0037;
         dimmer.alpha -=alpha;
    }
Run Code Online (Sandbox Code Playgroud)

Dou*_*ins 6

我将查看屏幕上当前所处的点,然后handlePanGesture:在视图中找到所占的百分比,CGFloat percentage = nowPosition.y/self.view.frame.size.height;然后将alpha设置为该点dimmer.alpha = 1.0 - percentage;。这样,无论您在何处移动,都将Alpha设置为与底部的接近程度。