How do I pass the touches events down to my viewcontroller? I think the UIScrollView is interecepting the touches and causeing the touches events that I have in the view controller not to fire
Code snippet:
//
// DragDrop.m
// Ballet
//
// Created by on 1/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "DragDrop.h"
#import "BAScrollView.h"
@implementation DragDrop
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self.view setBackgroundColor:[UIColor whiteColor]];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
sv = [[BAScrollView alloc] initWithFrame:self.view.frame];
[sv setBackgroundColor:[UIColor redColor]];
UIImageView *dragImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-thumb-10-mindful.png"]];
[dragImage2 setUserInteractionEnabled:YES];
dragImage2.frame = CGRectMake(0, 0, 64, 64);
[sv addSubview:dragImage2];
[self.view addSubview:sv];
//this image works as expected
UIImageView *dragImage3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-thumb-10-mindful.png"]];
[dragImage3 setUserInteractionEnabled:YES];
dragImage3.frame = CGRectMake(200, 200, 64, 64);
[self.view addSubview:dragImage3];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isKindOfClass:[UIImageView class]])
{
dragImage = [[UIImageView alloc] initWithImage:((UIImageView *)(touch.view)).image];
dragImage.frame = CGRectMake(100, 100, 64, 64);//touch.view.frame;
[dragImage setUserInteractionEnabled:YES];
// CGPoint point = [[[event allTouches] anyObject] locationInView:super.view];
//dragImage.frame =
// dragImage.center = point;
[self.view addSubview:dragImage];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"MOVED");
if (dragImage!=nil) {
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
dragImage.center = point;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (dragImage != nil)
{
[dragImage removeFromSuperview];
[dragImage release];
dragImage = nil;
}
}
@end
- the SCROLLVIEW SUB-CLASS
//
// BAScrollView.m
// Ballet
//
// Created by n 1/10/12.
// Copyright (c) 2012 MyCompanyName. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BAScrollView.h"
@implementation BAScrollView
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
@end
There is a good article about Responder Chain by Jeff Lamarche
In your case, you can subclass your scroll view and delegate your touch event to the next one in the responder chain.
This is not tested code, but I hope you get the idea.