还有许多其他问题,但似乎没有一个解决方案适合我。我是 iOS 的新手 - 目前正在处理 Big Nerd Ranch iOS Programming 一书中的问题。
我发现的大多数 SO 都说问题最终是 userInteractionEnabled = YES
was missing。或者 View 的背景设置为透明
。但是移除透明背景并设置 userInteractionEnabled = YES
并没有导致事件触发。有什么想法我想念的吗?
AppDelegate.m:
#import "AppDelegate.h"
#import "BNRHypnosisView.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect firstFrame = self.window.bounds;
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.userInteractionEnabled = YES;
ViewController *controller = [[ViewController alloc] init];
[self.window setRootViewController:controller];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
BNRHypnosisView.m:
#import "BNRHypnosisView.h"
@interface BNRHypnosisView()
@property (strong, nonatomic) UIColor *circleColor;
@end
@implementation BNRHypnosisView
-(void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
UIBezierPath *path = [[UIBezierPath alloc] init];
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20) {
[path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
[path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
}
path.lineWidth = 10;
[self.circleColor setStroke];
[path stroke];
}
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.circleColor = [UIColor lightGrayColor];
}
return self;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@ was touched", self);
float red = (arc4random() % 100) / 100.0;
float green = (arc4random() % 100) / 100.0;
float blue = (arc4random() % 100) / 100.0;
UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
self.circleColor = randomColor;
}
最佳答案
您正在将 BNRHypnosisView
添加为窗口的 subview 。稍后,当窗口即将出现在屏幕上时,它会将其 Root View Controller 的 View 作为另一个 subview 添加到您的催眠 View 前面。您可以在 View 层次结构中看到这一点,它在您的 BNRHypnosisView
之后显示一个普通的 UIView
。列表后面的 View 比列表前面的 View “在顶部”或“更靠近屏幕”。
试试这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *controller = [[ViewController alloc] init];
[self.window setRootViewController:controller];
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:controller.view.bounds];
firstView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
firstView.userInteractionEnabled = YES;
[controller.view addSubview:firstView];
[self.window makeKeyAndVisible];
return YES;
}
关于ios - UIView 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337020/