所有子视图,的TouchEvent在UIScrollView中
我有一个问题,我已经看过了所有可能的答案在计算器上(和其它互联网),但我尝试所有的解决方案,没有任何工程....
我有一个UIImageView作为子视图来创建一个板的UIScrollView。我可以放大和缩小,工作正常。也滚动工作正常!
现在,我想拖拽&将其他UIImageViews(图块)拖放到ScrollView中,同时也将ScrollView拖出。但是当我将UIImageView拖放到ScrollView中时,我无法拖放子视图。
我已经在viewDidLoad中设置了setCanCancelContentTouches:UIScrollView的NO。
我已经创建了一个子类TileImageView,并在init中将exclusiveTouch设置为YES。
注意: boardImage是一个'正常的'UIImageView',这些图块与子类一起!
此外:当我设置boardImage的UserInteraction设置为NO时,ScrollView会缩放但不会记录touchBegan。如果我将它设置为YES,ScrollView不会缩放,但touchBegan会被记录下来。
所以我离开它,所以我可以缩放,但是一旦它们在ScrollView中,它们不再可拖动。
我在做什么错?或者我错过了什么?
PS:设置delaysContentTouches属性使滚动无效的,这样也没有办法了......
代码: RootViewController_iphone.m
@implementation RootViewController_iphone
@synthesize boardScrollView;
@synthesize dragObject;
@synthesize boardImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
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;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIImageView *iView in self.view.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.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
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
{
UITouch *touch = [[event allTouches] anyObject];
NSString *tmp = touch.view.description;
CGPoint touchPointScreen = [[touches anyObject] locationInView:self.view];
CGPoint touchPointImage = [[touches anyObject] locationInView:self.boardImage];
CGPoint touchPointBoard = [[touches anyObject] locationInView:self.boardScrollView];
if (touchPointScreen.x > self.boardScrollView.frame.origin.x &&
touchPointScreen.x < self.boardScrollView.frame.origin.x + self.boardScrollView.frame.size.width &&
touchPointScreen.y > self.boardScrollView.frame.origin.y &&
touchPointScreen.y < self.boardScrollView.frame.origin.y + self.boardScrollView.frame.size.height)
{
self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x,
touchPointImage.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
[self.boardImage addSubview:self.dragObject]; // add to image
}
}
代码: TileImageView.m
@implementation TileImageView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
}
return self;
}
尝试通过为'UIImageView'启用'userinteraction'来尝试一次。 – Exploring
嗯,在界面生成器中,还有一个Interaction设置...... :(现在父视图中的触摸被记录,当我在de scrollview中触摸时...但现在拖放不再有效... – RobertvdBerg
有你在子视图中添加touch委托方法? – Exploring