【问题标题】:How to change a UIView background colour upon swipe?如何在滑动时更改 UIView 背景颜色?
【发布时间】:2017-08-19 18:41:47
【问题描述】:

如何通过 SwipeGesture 更改 UIView 背景颜色?例如绿色表示左滑,红色表示右滑。非常感谢。

代码不起作用:

- (void)viewDidLoad {

    self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:0.5];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [imageView setUserInteractionEnabled:YES];

    [super viewDidLoad];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on image view
    [self.view addGestureRecognizer:swipeLeft];
    [self.view addGestureRecognizer:swipeRight];
}

    - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

            self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
            NSLog(@"Left Swipe");
        }

        if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

            self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
            NSLog(@"Right Swipe");
        }

    }

【问题讨论】:

  • 您应该展示一些您尝试过但没有工作的代码,如果您没有尝试任何东西,我建议您在询问之前先做:)
  • 请在下面查看我的答案。
  • @TallentSiu 我为你添加了一个工作代码。请在下面查看。如果您发现它有效,我很感激投票和/或接受答案。 :) 乐于助人。

标签: ios objective-c xcode uiview uibackgroundcolor


【解决方案1】:

您需要有单独的滑动处理程序才能使其工作。这很好用。

您在设置 UISwipeGestureRecognizer 时的代码应如下所示 像这样。

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];

    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    imageView.userInteractionEnabled = YES;
    // Adding the swipe gesture on image view
    [imageView addGestureRecognizer:swipeRight];
    [imageView addGestureRecognizer:swipeLeft];

你的滑动手柄应该是这样的。

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
    self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)swipe {
    self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
}

【讨论】:

    【解决方案2】:

    在 ViewController 文件的interface 中声明一个 imageView 属性。 @property (weak, nonatomic) UIImageView *imageView;

    - (void)viewDidLoad
        {
        [super viewDidLoad];
      self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
        [imageView setUserInteractionEnabled:YES];
    
        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    
        // Setting the swipe direction.
        [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
        [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    
        // Adding the swipe gesture on image view
        [self.imageView addGestureRecognizer:swipeLeft];
        [self.imageView addGestureRecognizer:swipeRight];
    
        }
    

    处理滑动手势事件 //newImage 和 newImage1 是 projectImageAssets 中的图像文件名 - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
            NSLog(@"Left Swipe");
    self.view.backgroundColor = [UIColor greenColor];
    self.imageView.image = [UIImage imageNamed: @"myNewImage.png"];
        }
    
        if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
            NSLog(@"Right Swipe");   
        self.view.backgroundColor = [UIColor redColor];
        self.imageView.image = [UIImage imageNamed: @"myNewImage1.png"];
        } 
    
    }
    

    希望这会有所帮助。快乐编码。 refer to this SO Post

    【讨论】:

    • 感谢您的回复,但您的代码不适用于我的情况,因为它会首先更改 imageView,然后仅更改第二次滑动的背景颜色。我可以通过一次滑动同时更改 imageView 和背景颜色吗?谢谢。
    • 是的,你想什么时候改变 imageView
    • 您需要将imageView声明为属性并更改图像。请检查我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2013-03-14
    相关资源
    最近更新 更多