所有,

我有一个问题,我在StackOverflow(和其他Internet)上查看了所有可能的答案,但是我尝试的所有解决方案都无法解决。

我有一个UIScrollView和一个UIImageView作为子视图来创建板。我可以放大和缩小,效果很好。滚动也很好!

现在,我想将其他UIImageViews(平铺)拖放到ScrollView以及ScrollView的OUT中。但是,当我将UIImageView放置在ScrollView中时,无法拖放该子视图。

我已经在viewDidLoad中设置了UIScrollView的setCanCancelContentTouches:NO。
我创建了一个子类TileImageView,并在初始化中将ExclusiveTouch设置为YES。
注意:boardImage是一个'normal'UIImageView',图块带有子类!

另外:当我将boardImage的UserInteraction设置设置为NO时,它的ScrollView会缩放,但不会记录touchBegan。如果将其设置为“是”,则ScrollView不会缩放,但会记录touchBegan。

因此,我将其保留下来以便进行缩放,但是Tiles一旦位于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的子类,因此可以在子类中执行以下操作:

1-覆盖触摸开始,didEnd和didMove
2-在这些方法内将它们传递给UIScrollView的超级视图:[self.superview touchsBegan .... ]
3-如果您的UIImageView没有收到这些消息,请在init中添加self.userInteractionEnabled = YES;
希望这对您有帮助

关于ios - UIScrollView中 subview 的touchEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21676227/

10-12 12:47
查看更多