以下代码行表示我插入到滚动视图中的对象。
我的问题是,滚动视图中的所有图像都不是可拖放的。
当单独添加图像时,即在滚动视图之外,它们可以完美地单击和拖放。
似乎每个发生的事件都会触发 scrollViews 侦听器,而不是单击的图像侦听器。
无论图像如何添加,scrollView 都可以完美滚动。
如果单击,我如何触发图像拖放,并且仍然能够滚动它的父视图 - 滚动视图?
#import "BookShelfSection.h"
@implementation BookShelfSection
- (id)initBookShelfWithX:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height
{
self = [super initWithFrame:CGRectMake(x, y, width, height)];
self.userInteractionEnabled = YES;
return self;
}
- (void)addPages:(NSArray *)fileNameArray
{
for (id shelf in fileNameArray) {
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, self.frame.size.width, self.frame.size.height)];
UIImage *image = [UIImage imageNamed:[NSString stringWithString:shelf]];
UIImageView *page = [[UIImageView alloc] initWithImage:image];
page.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
page.contentMode = UIViewContentModeBottom;
UIImage *image2 = [UIImage imageNamed:@"car_1.png"];
Item *item = [[Item alloc] initWithImage:image2 andPosition:CGPointMake(100, 100)];
page.userInteractionEnabled = YES;
item.userInteractionEnabled = YES;
[view addSubview:page];
[view addSubview:item];
[self addPageView:view];
}
}
@end
编辑:这就是我执行拖放操作的方式:项目类:
#import "Item.h"
@implementation Item
- (id)initWithImage:(UIImage *)image andPosition:(CGPoint)position
{
CGRect cellRectangle = CGRectMake(position.x, position.y,image.size.width ,image.size.height );
self = [[Item alloc] initWithFrame:cellRectangle];
if (self) {
[self setImage:image];
[self setUserInteractionEnabled:YES];
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self setFrame:frame];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect frame = [self frame];
int originX = (int)roundf(frame.origin.x);
int originY = (int)roundf(frame.origin.y);
NSLog(@"Touch ended at point: [%d, %d]", originX, originY);
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch cancelled");
}
@end