我有一个UIScrollView(boardScrollView)一个RootViewController的。 这有一个UIImageView作为子视图创建板(boardImage)。 我可以放大和缩小,做工精细。 还滚动工作正常!
现在,我想拖放其他的UIImageViews(砖)进出滚动型的。
我有子类的UIImageView到TileImageView和覆盖方法的touchesBegan,touchesMoved和touchesEnded。 拖放对上海华能正常工作。 但我希望能够拖放到滚动型这是对一个RootViewController的IBOutlet中的瓷砖。
我可以拖放的瓷砖,因为我可以用superview
,但我不知道我可以平铺添加到boardScrollView。
任何人都知道我怎样才能从子类中的RootViewController的访问的对象?
我应该委托什么? 也可以设置不同的参数?
代码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;
}
典子TileImageView.h:
#import <UIKit/UIKit.h>
@interface TileImageView : UIImageView
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@end
典子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....
}