I have a RootViewController with a UIScrollView (boardScrollView). This has a UIImageView as subview to create a board (boardImage). I can zoom in and out, works fine. Also scrolling works fine!
Now, I want to drag & drop other UIImageViews (Tiles) into and out of the ScrollView.
I have subclassed UIImageView to TileImageView and overridden methods touchesBegan, touchesMoved and touchesEnded. The drag & drop works fine on the superview. But I want to be able to drop the Tiles on the ScrollView which is a IBOutlet on the RootViewController.
I can drag and drop the tile because I can use superview
but I don't know how I can add the Tile to the boardScrollView.
Anybody knows how I can access objects on the RootViewController from the subclass?
Am I supposed to delegate something? Or set a different parameter?
Code RootViewController:
@implementation RootViewController_iphone
@synthesize boardScrollView;
@synthesize dragObject;
@synthesize boardImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.boardScrollView.clipsToBounds = YES;
UIImage *image = [UIImage imageNamed:@"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];
self.boardImage.userInteractionEnabled = YES;
self.boardScrollView.contentSize = image.size;
self.boardScrollView.userInteractionEnabled = YES;
self.boardScrollView.canCancelContentTouches = NO;
}
Code subclass TileImageView.h:
#import <UIKit/UIKit.h>
@interface TileImageView : UIImageView
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@end
Code subclass TileImageView.m:
#import "TileImageView.h"
@implementation TileImageView
@synthesize dragObject;
@synthesize touchOffset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
// for now, I use the superview, but to be able to drag from the ScrollView,
I need to access the boardScrollView on the UIViewController...
// i.e CGPoint touchPoint = [[touches anyObject] locationInView:self.boardScrollView];
for (UIImageView *iView in self.superview.subviews) {
if ([iView isMemberOfClass:[TileImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
[self.superview bringSubviewToFront:self.dragObject];
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
// here I use the superview, but I also want to use the ScrollView
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// here I need to be able to use the ScrollView on the RootViewController....
}