【问题标题】:Touch event on subviews on UIScrollViewUIScrollView 子视图上的触摸事件
【发布时间】:2014-02-19 09:04:46
【问题描述】:

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

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

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

【问题讨论】:

  • 试试我的答案...它会工作

标签: ios objective-c uiscrollview


【解决方案1】:

你必须制作 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

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多