Preston A. Perriott
Preston A. Perriott

Reputation: 3

Button press functionality won't work/ Button won't press Objective C

So im not necessarily sure what my exact problem is. But by reading up on some questions asked here & I think I've been able to narrow my issue down to a couple of problems. Either I have too many layers over the button itself, or I've personally screwed up on the raw code. A little assistance would be absolutely lovely, thank you very much.

//Create an image pointer, initializes itself to fit the frame of the screen
UIImageView *picView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
picView.image = [UIImage imageNamed:@"god_and_devil_playing_cards-wallpaper-1366x768.jpg"];
[self.view addSubview:picView];
[self.view sendSubviewToBack:picView];

//Creating a filter over the original imagine so buttons can be seen easier
UIView * filter = [[UIView alloc] initWithFrame:self.view.frame];
filter.backgroundColor = [UIColor blackColor];
filter.alpha = 0.5;
[self.view addSubview:filter];

//Many steps to create a pulsating button
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(90, 135, 25, 25);  //left/right, up/down, height, width
//btn1.center = self.view.center;
[btn1 setTitle:@"" forState:UIControlStateNormal];

//Adds a sub view to the button, the size of the button
UIView *c = [[UIView alloc] initWithFrame:btn1.bounds];
c.backgroundColor = [UIColor blackColor];
c.layer.cornerRadius = 12.5;
[btn1 addSubview:c];
[btn1 sendSubviewToBack:c];

UIView *f = [[UIView alloc] initWithFrame:btn1.bounds];
f.backgroundColor = [UIColor grayColor];
f.layer.cornerRadius = 12.5;
[btn1 addSubview:f];
[btn1 sendSubviewToBack:f];

//Creating the functionality of the pulsing movement
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = MAXFLOAT;
[c.layer addAnimation:pulseAnimation forKey:@"a"];
[btn1.titleLabel.layer addAnimation:pulseAnimation forKey:@"a"];

CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
fade.toValue = @0;
CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulse.toValue =@2;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[fade,pulse];
group.duration = 2.0;
group.repeatCount = MAXFLOAT;
[f.layer addAnimation:group forKey:@"g"];

[btn1 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn1.userInteractionEnabled = YES;

//Second pulsating button over the devils head
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(260, 175, 25, 25);
// Centers button to middle of screen btn1.center = self.view.center;
[btn2 setTitle:@"" forState:UIControlStateNormal];

UIView *q = [[UIView alloc] initWithFrame:btn2.bounds];
q.backgroundColor = [UIColor grayColor];
q.layer.cornerRadius = 12.5;
[btn2 addSubview:q];
[btn2 sendSubviewToBack:q];

UIView *z = [[UIView alloc] initWithFrame:btn2.bounds];
z.backgroundColor = [UIColor blackColor];
z.layer.cornerRadius = 12.5;
[btn2 addSubview:z];
[btn2 sendSubviewToBack:z];

[q.layer addAnimation:pulseAnimation forKey:@"a"];
[btn2.titleLabel.layer addAnimation:pulseAnimation forKey:@"a"];

[z.layer addAnimation:group forKey:@"a"];

[self.view addSubview:btn2];
}

-(void)action:(UIButton *) btn1{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert!!" message:@"Fuck off User" preferredStyle:UIAlertControllerStyleAlert];
    //Creates the action button within the alert
    UIAlertAction * defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
    UIAlertAction * anotherAction = [UIAlertAction actionWithTitle:@"Exit" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action){}];


    //Adds action button "OK" to the alert popup
    [alert addAction:defaultAction];
    //Adds destructive button exit to alert popup
    [alert addAction:anotherAction];
    //Adds alert to the view controller making it an actual thing
    [self presentViewController:alert animated:YES completion:nil];

}

Upvotes: 0

Views: 83

Answers (1)

Michael
Michael

Reputation: 6907

You need to set userInteractionEnabled to NO on your subviews as this property on UIViews defaults to YES. This will allow the touches to pass through the views to your UIButton.

Upvotes: 1

Related Questions