在平移手势或拖动时更改视图 Alpha [英] Change View Alpha on Pan Gesture or Dragging

查看:49
本文介绍了在平移手势或拖动时更改视图 Alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 UIView 在 Pan Gesture 上拖动到屏幕底部,但是当它到达屏幕底部时,视图 alpha 应该缩小到零".

I want to a UIView to drag to bottom of the screen on Pan Gesture but also the view alpha should scale down to "zero", when it reaches to the bottom of the screen.

反之亦然,当我向上拖动视图时,UIView alpha 应该缩小到1"

And vise versa, when I will drag the view upwards then the UIView alpha should scale down to "1"

但问题是视图的 alpha 在平移一半屏幕时或有时在我较慢地拖动视图时缩小到零".
最初我将 UIView 背景颜色设为黑色.

But the problem is that the view's alpha is scaling down to "Zero" on panning half of the screen or sometimes when I drag the view slower.
Initially I have made the UIView background color to Black.

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

I need to scale down the alpha of the view gradually , any idea or suggestion will be helpful.

  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;
    }

推荐答案

我会在 handlePanGesture: 中查看您当前所在的屏幕点,找到您在视图中的百分比CGFloat percent = nowPosition.y/self.view.frame.size.height; 然后将 alpha 设置为 dimmer.alpha = 1.0 - percent;.这样无论您移动到哪里,您都可以将 alpha 设置为离底部有多近.

I would look at the point on the screen you are currently at inside your handlePanGesture: find the percentage you are at on the view CGFloat percentage = nowPosition.y/self.view.frame.size.height; then set the alpha to that dimmer.alpha = 1.0 - percentage;. This way no matter where you are moving, you are setting the alpha to how close to the bottom you are.

这篇关于在平移手势或拖动时更改视图 Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆