不同iOS SDK对事件调用支持不同

问题描述:

今天做AD杂志(iPad)LEGO功能点,需要在UIScrollView中让UIView移动。加了正确代码,结果却不好。

结果是这样的:点击不移动,运行touchesBegan和touchesEnded,一切正常。但是点下移动释放,touchesBegan会100%运行,然后三两个touchesMoved,move效果也跟不上,touchesEnded也没有触发。

在UIView用了如下代码实现:

#pragma mark --

#pragma mark touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesBegan");

UITouch *touch = [touches anyObject];

UIScrollView *parentView = (UIScrollView *)[[[touch view] superview]superview];

parentView.scrollEnabled=NO;

// Calculate and store offset, and pop view into front if needed

CGPoint pt = [[touches anyObject] locationInView:self];

startLocation = pt;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesMoved");

// Calculate offset

CGPoint pt = [[touches anyObject] locationInView:self];

float dx = pt.x - startLocation.x;

float dy = pt.y - startLocation.y;

CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);

// Bound movement into parent bounds

float halfx = CGRectGetMidX(self.bounds);

newcenter.x = MAX(halfx, newcenter.x);

newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);

float halfy = CGRectGetMidY(self.bounds);

newcenter.y = MAX(halfy, newcenter.y);

newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);

// Set new location

self.center = newcenter;

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesEnded");

UITouch *touch = [touches anyObject];

UIScrollView *parentView = (UIScrollView *)[[[touch view] superview]superview];

parentView.scrollEnabled=YES;

}

问题分析:

最后一段一段检查,应该是parentView.scrollEnabled=NO;这句有问题。

可能是iOS的一个Bug。

问题解决:

在另一台Mac上做了一个Demo,一切OK。细细检查,唯一不同就是SDK版本不一样。出问题的那台是4.1,而做Demo一切OK的那台是4.2.1。把出问题的代码copy给4.2.1,一切安逸了。