1

20 個のサブビューをscrollview行ごとに行として追加しました

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)];
    lbltime.text=@"VIEW HERE";
    lbltime.textColor=[UIColor grayColor];
  //  [timeView addSubview:lbltime];
    [scrlView addSubview:timeView];

    yPos=yPos+61;
}

サブビューをタップすると、タップされたサブビューのプロパティが取得されません。

みたいなコーディネート。親ビューの座標を与えています

サブビューUserInteractionEnabledをはいに有効にしました。

タップされたサブビューの座標とタグの値を取得する方法を教えてもらえますか?

4

3 に答える 3

1

からサブクラス化しないUIScrollViewでください。これがまさにジェスチャー認識機能がある理由です。また、各ビューに個別のジェスチャ レコグナイザーを追加しないでください。

スクロール ビューにジェスチャ レコグナイザーを 1 つ追加し、クリックされたときにタッチの 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
}
于 2013-08-21T07:50:40.733 に答える
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
        }
    } 
}
于 2013-08-21T07:15:25.010 に答える