In Xcode 7 can't see message "was touched" in console


#1

In Xcode 7 can’t see message “was touched” in console that called from method touchesBegan

[code]//
// BNRHypnosisView.m
// Quiz
//
// Created by admin on 12.11.15.
// Copyright © 2015 Givi Pataridze. All rights reserved.
//

#import “BNRHypnosisView.h”

@interface BNRHypnosisView ()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {
    CGRect bounds = self.bounds;
    CGRect imageBounds = CGRectMake(100, 150, 120, 220);
    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];
    UIImage *logoImage = [UIImage imageNamed:@“logo.png”];

    [logoImage drawInRect:imageBounds];
    }

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor clearColor];
    self.circleColor = [UIColor lightGrayColor];
    }
    return self;
    }

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"%@ was touched", self);

    // Get 3 random numbers between 0 and 1
    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;
    }

@end
[/code]

I guess that that means:

  1. Or In simulator I can’t generate touch event
  2. Or This event don’t come until this line in code NSLog(@"%@ was touched", self);

#2

Problem was resolved after: or installing new Xcode 7.1.1 or after system reboot.
All works correctly, thanks!


#3

I’m having a similar issue to DreamWalker running XCode 7.1.1 - the app doesn’t respond to the touchesBegan event.

developer.apple.com/library/ios … :withEvent: states that a super event has to be called, so I’ve added [super touchesBegan:touches withEvent:event]; to the method and changed the call to the method to - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event. But, still nothing.

I’ve changed AppDelegate as suggested in one of the other posts on this site - perhaps there’s something I don’t understand?

Any ideas?

Thanks,

David

BNRHypnosisView.m:

#import "BNRHypnosisView.h"

@interface BNRHypnosisView()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView

- (instancetype)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.circleColor = [UIColor lightGrayColor];
    }
    return self;
}

// when a finger touches the screen
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    NSLog(@"%@ was touched", self);
    
    // get 3 random number between 0 and 1
    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;
}

- (void)drawRect:(CGRect)rect
{
    CGRect bounds = self.bounds;
    
    // figure out the centre of the bounds rectangle
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    
    // the circle will be the largest that will fit into the view
//    float radius = MIN(bounds.size.width, bounds.size.height) / 2.0;
    
    // the largest circle will cirumscribe the view
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    // add an arc to the path at centre with radius of radius from 0 to 2*PI radians
//    [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
    
    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];
    }
    
    // configure the line width
    path.lineWidth = 10;
    
    // configure the drawing colour
//    [[UIColor lightGrayColor] setStroke];
    [self.circleColor setStroke];
    
    // draw the line
    [path stroke];
}

@end

Partial AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    UIViewController *viewController = [[UIViewController alloc] init];
    [self.window setRootViewController:viewController];
    
    CGRect firstFrame = self.window.bounds;
    
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    
    [self.window addSubview:firstView];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

#4

Finally manged to get this working using the AppDelegate.m file specified by oztheozoz in viewtopic.php?f=485&t=10511#wrap


#5

I am not able to see “was touched” in console. Nothing happens when I click on simulator using touchpad.

I believe touch event is not working on my Simulator.

Do I need to change any setting for this ?


#6

I have faced with the same issue.
And after small changes in application:didFinishLaunchingWithOptions: method of BNRAppDelegate.m - everything began to work fine.
See code below:

BNRAppDelegate.h

[code]#import <UIKit/UIKit.h>

@interface BNRAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end
[/code]
BNRAppDelegate.m
To make all things works, be sure that you create UIViewController and set it to the self.window.rootViewController; also it’s important to addSubview to self.window.rootViewController.view (not self.window)

[code]#import “BNRAppDelegate.h”
#import “BNRHypnosisView.h”

@implementation BNRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UIViewController alloc] init];
    self.window.backgroundColor = [UIColor whiteColor];

    CGRect firstFrame = self.window.bounds;
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];

    [self.window.rootViewController.view addSubview:firstView];

    return YES;
    }[/code]
    BNRHypnosisView.m

[code]#import “BNRHypnosisView.h”

@interface BNRHypnosisView ()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor clearColor];
    self.circleColor = [UIColor lightGrayColor];
    }
    return self;
    }

  • (void)setCircleColor:(UIColor *)circleColor
    {
    _circleColor = circleColor;
    [self setNeedsDisplay];
    }

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"%@ was touched", self);

    // Get 3 random numbers between 0 and 1
    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;
    }

  • (void)drawRect:(CGRect)rect
    {
    // the same implementation as in book
    }[/code]


#7

thank you so much!!! changed [self.window addSubview:firstView] to [self.window.rootViewController.view addSubview:firstView];
and now everything works

[quote=“amelia”]I have faced with the same issue.
And after small changes in application:didFinishLaunchingWithOptions: method of BNRAppDelegate.m - everything began to work fine.
See code below:

BNRAppDelegate.h

[code]#import <UIKit/UIKit.h>

@interface BNRAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end
[/code]
BNRAppDelegate.m
To make all things works, be sure that you create UIViewController and set it to the self.window.rootViewController; also it’s important to addSubview to self.window.rootViewController.view (not self.window)

[code]#import “BNRAppDelegate.h”
#import “BNRHypnosisView.h”

@implementation BNRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UIViewController alloc] init];
    self.window.backgroundColor = [UIColor whiteColor];

    CGRect firstFrame = self.window.bounds;
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];

    [self.window.rootViewController.view addSubview:firstView];

    return YES;
    }[/code]
    BNRHypnosisView.m

[code]#import “BNRHypnosisView.h”

@interface BNRHypnosisView ()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor clearColor];
    self.circleColor = [UIColor lightGrayColor];
    }
    return self;
    }

  • (void)setCircleColor:(UIColor *)circleColor
    {
    _circleColor = circleColor;
    [self setNeedsDisplay];
    }

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"%@ was touched", self);

    // Get 3 random numbers between 0 and 1
    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;
    }

  • (void)drawRect:(CGRect)rect
    {
    // the same implementation as in book
    }[/code][/quote]