2013-08-21 16 views
1

我已經通過線加20子視圖scrollview線行我們可以從iOS中滾動查看

yPos=0; 
    for (int i=0; i<24; i++) { 

    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, yPos, 909, 60)]; 
    timeView.userInteractionEnabled=TRUE; 
    timeView.exclusiveTouch=YES; 
    timeView.tag=i; 
    NSLog(@"sub vieww tag=:%d",timeView.tag); 
    timeView.backgroundColor=[UIColor whiteColor]; 
    UILabel *lbltime=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 60)]; 
    [email protected]"VIEW HERE"; 
    lbltime.textColor=[UIColor grayColor]; 
    // [timeView addSubview:lbltime]; 
    [scrlView addSubview:timeView]; 

    yPos=yPos+61; 
} 

現在觸摸子視圖屬性時,曾經我在一個子視圖點擊我沒有得到抽頭子視圖屬性。

like coordinates。這是給父視圖座標

我啓用子視圖UserInteractionEnabled爲是。

任何人可以告訴我如何獲取挖掘子視圖的座標和標籤值嗎?

+0

加點觸手勢與您所添加的意見,並連同基於在tapgetsure事件方法標籤添加標籤的意見,並確定視圖。 –

+0

我建議你編輯帖子以顯示你用來處理水龍頭的代碼。如果你正在使用輕觸手勢識別器,它附加了什麼 - 子視圖或滾動視圖或什麼? – RegularExpression

+0

tapgetsure事件如何獲取挖掘視圖.'UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap :)]; tap.numberOfTapsRequired = 1;如何獲得點擊的視圖。我已經爲每個子視圖設置了標籤 – sudheer

回答

1

不要從UIScrollView繼承,這就是爲什麼有手勢識別器。另外,請勿爲每個視圖添加單獨的手勢識別器。

將一個手勢識別器添加到您的滾動視圖中,單擊時使用觸摸的x,y值來計算單擊哪個視圖。 您需要做一個小計算:(y value of the click + UIScrollView y offset)/60。 60是每個視圖的高度。這應該返回點擊視圖的索引。

編輯:

代碼示例:

- (void)viewTapped:(UIGestureRecognizer*)recognizer 
{ 
    CGPoint coords = [recognizer locationInView:recognizer.view]; 
    int clickedViewIndex = (self.offset.y + coords.y)/60; 

    // now clickedViewIndex contains the index of the clicked view 
} 
+0

Thanks.explain,根據我的代碼簡要示例代碼.. – sudheer

+0

Eli ganem我可以簡要解釋一下 – sudheer

0

使類擴展的UIScrollView:

例如:

.h file : 

@protocol CustomScrollViewDelegate <NSObject> 

@optional 
// optional becuase we always don't want to interact with ScrollView 

- (void) customScrollViewTouchesEnded :(NSSet *)touches withEvent:(UIEvent *)event; 

- (void) customScrollViewDidScroll; 


@end 

@interface CustomScrollView : UIScrollView 

@property (weak, nonatomic) id<CustomScrollViewDelegate> touchDelegate; 
// delegate was giving error becuase name is already there in UIScrollView 

@end 

代碼.m文件:

#import "CustomScrollView.h" 

@implementation CustomScrollView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touchesEnded"); 
    if (!self.dragging) { 
     //NSLog(@"touchesEnded in custom scroll view"); 
     if (_touchDelegate != nil) { 
      if ([_touchDelegate respondsToSelector:@selector(customScrollViewTouchesEnded:withEvent:)]) { 
       [_touchDelegate customScrollViewTouchesEnded:touches withEvent:event]; 
      } 
     } 
    } 

    else { 
     // it wouldn't be called becuase at the time of dragging touches responding stops. 
     [super touchesEnded:touches withEvent:event]; 
    } 

} 

@end 

實現滾動視圖的這種使用子視圖,它會工作

for (UILabel *label in [customScrollView subviews]) { // change name of table here 
    if ([label isKindOfClass:[UILabel class]]) { 
     if (label.tag == savedtag) { // use your tag 
      // write the code as desired 
     } 
    } 
} 
1
  UIView *v = recognizer.view; 
      int tagNum = [v tag]; 

      Using the tagNum you can do your further operatins. 
     Or v is your object of tapped view. 

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 
tap.numberOfTapsRequired = 1; 
[timeview addGestureRecognizer:tap]; 

    Add this in for loop only. 
+0

我又遇到了一個問題。如何在滾動視圖的子視圖上添加視圖? – sudheer

+0

[subview addSubView:view];我的滾動視圖中有 –

+0

有20個子視圖,可以在任何兩個或三個子視圖上添加視圖。這些子視圖已經是滾動視圖的子視圖 – sudheer