按钮按下功能不起作用/按钮无法按下 Objective C

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

所以我不确定我的确切问题是什么。但是通过阅读这里提出的一些问题,我想我已经能够将我的问题缩小到几个问题。要么我在按钮本身上有太多层,要么我个人搞砸了原始代码。一点帮助将是绝对可爱的,非常感谢。

//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];

}

您需要在子视图上将 userInteractionEnabled 设置为 NO,因为 UIView 上的 属性 默认为 YES。这将允许触摸通过视图传递到您的 UIButton