如何在滑动时更改UIView背景颜色?

时间:2017-03-27 07:08:42

标签: ios objective-c xcode uiview uibackgroundcolor

如何通过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");
        }

    }

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

在ViewController文件的interface中声明imageView属性。     @property(弱,非原子)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 *)刷卡{

    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