【发布时间】:2015-10-26 01:12:31
【问题描述】:
还有许多其他问题,但似乎没有一个解决方案对我有用。我对 iOS 很陌生 - 目前正在解决 Big Nerd Ranch iOS 编程书中的一个问题。
我发现的大多数 SO 都说问题最终是 userInteractionEnabled = YES 丢失。或者视图的背景设置为transparent。但是移除透明背景并设置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;
}
【问题讨论】:
-
你还有其他关于催眠观点的观点吗?你能通过在视图调试器中看到它来确认它吗!!!!
-
嗯。这是我第一次使用视图调试器,我刚刚发布的截图中的UIView实际上是在催眠视图前面吗?
-
你贴的截图是在催眠视图上面吗?
-
看起来是这样的?但是我没有在我的代码中的任何地方添加 UIView,我可以说,Hypnosis 视图是 UIView 的子类 :(。知道这可能来自哪里吗?控制器中是否添加了某种默认视图?
-
这可能与幕后还有故事板有关吗?
标签: ios objective-c uiview touchesbegan