所有
我有一个问题,我已经在StackOverflow(和其他互联网)上查看了所有可能的答案,但我尝试了所有解决方案,没有任何作用....
我有一个带有UIImageView的UIScrollView作为子视图来创建一个板。我可以放大和缩小,工作正常。滚动效果也很好!
现在,我想拖动&将其他UIImageViews(图块)拖放到ScrollView以及ScrollView的OUT中。但是当我在ScrollView中删除UIImageView时,我无法拖放子视图。
我在viewDidLoad中设置了setCanCancelContentTouches:UIScrollView的NO。 我创建了一个子类TileImageView,在init中我将exclusiveTouch设置为YES NB: boardImage是一个'普通的'UIImageView',这些图块是子类的!
添加:当我将boardImage的UserInteraction设置设置为NO时,ScrollView会进行缩放,但不会记录touchBegan。如果我将其设置为YES,则ScrollView不会缩放,但会记录touchBegan ..
所以我离开它以便我可以缩放,但是一旦它们在ScrollView中,Tiles就不再可拖动了。
我究竟做错了什么?或者我错过了什么?
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;
}
答案 0 :(得分:1)
您已经有子类UIImageView,因此您可以在子类中执行以下操作:
1-覆盖touchsBegan,didEnd和didMove
这些方法中的2-将它们传递给超级视图UIScrollView
:[self.superview touchsBegan .... ]
3-如果你的UIImageView没有收到这些消息,那么在init中添加self.userInteractionEnabled = YES;
希望这会帮助你