繁体   English   中英

在UIScrollView的子视图上触摸事件

[英]Touch event on subviews on UIScrollView

我在UIScrollView中创建了许多子视图。 让我将每个子视图称为Cell 每个单元中都有一个UILabel。

然后,我在Cell类上实现touchEnd方法,希望我的应用程序在我点击任何一个单元格时都能响应我的触摸。 但是,我无法从所有单元获得响应。 我只能从ScrollView中手机底部上方的单元格(iPhone 5为568)获得响应,而ScrollView的contentsize为2300 * 1000,这意味着y = 568以下的单元格不响应我的感动。 我怎样才能解决这个问题? 这是我在Cell类中的touchEnd方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (![self.unit.text isEqualToString:@"?"]) {
        [self.soundManager playSoundForUnit:self.unit];
    }
}

您必须制作CustomScrollView:

。H :

@protocol CustomScrollViewDelegate <NSObject>

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

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

@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
{

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM