【发布时间】:2021-08-25 01:07:58
【问题描述】:
基本上我在这里要做的是有一个函数,它将 self.view 的子视图的 alpha 设置为 1(默认情况下它是隐藏的),它在按下按钮时被调用。我遇到过几次这个问题,因为我在 viewDidLoad 中声明了子视图,但我不知道如何与不同函数中的子视图进行交互(我试过 self.view.subviews[n] 但 xcode 不是对此感到高兴)。我能想到的唯一另一种方法是将self.view设置为我想要的视图,或者甚至可以在我希望用按钮调用的函数中创建视图(我看到的问题可能是视图不会覆盖我想要发生的按钮)。
这是我目前所拥有的:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *firstBackground = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIView *blackOverlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
blackOverlay.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
firstBackground.backgroundColor = [UIColor whiteColor];
UIButton *fakeNotifButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fakeNotifButton addTarget:blackOverlay action:@selector(setupFakeNotifScreen:) forControlEvents:UIControlEventTouchUpInside];
//i haven't finished the button yet, that i don't need any help with
}
-(IBAction)setupFakeNotifScreen:(id)sender {
[UIView animateWithDuration:1.0
delay:0.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{view = 1.0;} //here is where i want to call blackOverlay
completion:^(BOOL finished){}];
}
@end
编辑:我想通了...甚至不需要 blackOverlay 视图。这是任何未来谷歌人的解决方案......可能有更有效的方法来做到这一点,所以我会感谢任何比我更好的人:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *fakeNotifButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fakeNotifButton addTarget:self action:@selector(setupFakeNotifScreen:) forControlEvents:UIControlEventTouchUpInside];
fakeNotifButton.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - 80, 300, 160, 40);
[fakeNotifButton setTitle:@"test" forState:UIControlStateNormal];
[fakeNotifButton setBackgroundColor:[UIColor systemGreenColor]];
fakeNotifButton.layer.cornerCurve = @"continuous";
fakeNotifButton.layer.cornerRadius = 15;
[self.view addSubview:fakeNotifButton];
}
-(void)setupFakeNotifScreen:(UIButton *)sender {
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{self.view.backgroundColor = [UIColor blackColor]; sender.alpha = 0.0;}
completion:^(BOOL finished){}];
}
@end
【问题讨论】:
-
请不要标记垃圾邮件。仅使用您实际编码的语言标签。
-
kaylum 很糟糕,那些在建议的标签下,所以我认为没关系
标签: ios objective-c iphone