I am trying to use a gradient as a background to my UIView.

    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = CGRectMake(0.f, 0.f, self.currencyView.frame.size.width, self.currencyView.frame.size.height);
    [gradientLayer setLocations:@[@0.f, @1.f]];
    [gradientLayer setColors:@[(id)[UIColor whiteColor].CGColor,(id)[UIColor blackColor].CGColor]];
    [self.animalView.layer insertSublayer:gradientLayer atIndex:0];

I have made this view tappable by adding a UITapGestureRecognizer

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAnimalDetails)];
    [self.currencyView addGestureRecognizer:singleFingerTap];

My question is, how can I add a highlighted gradient (possibly just reversing the gradient) when the touch was detected.

Any help would be greatly appreciated.

有帮助吗?

解决方案

Make that layer a class variable instead of local, then inside your showAnimalDetails function change the gradient properties however you wish. Then call setNeedsDisplay. You are going to have to deal with deselection by other means though.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top