2013-04-25 66 views
0

以下代碼行代表我插入到scrollView中的對象。iOS:帶有可點擊對象的ScrollView

我的問題是,scrollview內的圖像都不能拖放。

當單獨添加圖像時,即在滾動視圖之外添加圖像時,它們是完美的點擊 - 拖放功能。

它出現的每個事件都會觸發scrollViews偵聽器,而不是點擊的圖像偵聽器。

無論圖像如何添加,scrollView都是完全可滾動的。

我怎樣才能觸發圖像拖放,如果點擊,仍然能夠滾動它的父視圖 - 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 
+0

put scrollview.userInteractionEnabled = YES;併爲圖像 – Balu 2013-04-25 05:50:02

+0

編輯,還沒有運氣。謝謝 – Eyeball 2013-04-25 05:56:47

+0

你如何進行拖放? – 2013-04-25 05:57:35

回答

0

它添加到的UIImageView:

page.userInteractionEnabled = YES; 
item.userInteractionEnabled = YES; 
+0

編輯:雖然沒有進展。 – Eyeball 2013-04-25 05:55:56

0

它出現每個發生的事件觸發scrollViews監聽器,而不是點擊圖像監聽河

當你捕捉到這個事件的滾動視圖,循環通過你添加的頁面/圖像&檢查userInteractionEnabled的值?可能會有助於縮小問題的範圍。