2014-06-26 98 views
0

这里是我的代码:IOS滚动视图无法点击

int i = 0; 
self.userInteractionEnabled = YES; 
self.exclusiveTouch = YES; 


self.canCancelContentTouches = YES; 
self.delaysContentTouches = YES; 
self.translatesAutoresizingMaskIntoConstraints = YES; 
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(gototest)]; 
tapGestureRecognizer.numberOfTapsRequired = 1; 
[self addGestureRecognizer:tapGestureRecognizer]; 
for (NSString *message in self.messages) { 
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    self.userInteractionEnabled = YES; 
    label.text = message; 
    label.tag = i; 
    CGSize size = [message sizeWithFont:label.font]; 
    CGFloat width = size.width + kPADDING; 
    label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height); 
    [self addSubview:label]; 

    i++; 

    xPos += width; 
    NSLog(@"%@",NSStringFromCGRect(label.frame)); 
} 
self.messagesWidth = xPos; 
self.contentSize = CGSizeMake(xPos, self.frame.size.height); 
self.contentOffset = CGPointMake(-self.frame.size.width, 0.0); 

} 

-(void)gototest 
{ 
NSLog(@"test %@",@"ccc "); 
} 

,然后选取框 - (无效)去{

if (!self.period) self.period = self.messagesWidth/100; 
// so it always takes about the same (fudged, but reasonable) amount of time to scroll the whole array 

[UIView animateWithDuration:self.period 
         delay:0.0 
        options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionRepeat 
       animations:^{ 
        self.contentOffset = CGPointMake(self.messagesWidth, 0.0); 
       } completion:^(BOOL finished){}]; 
} 

所以,我的目标是创建新闻选框,每个新闻都可以点击查看点击新的详细信息。

但UITapGestureRecognizer不工作,我不知道为什么。

请注意,自我是滚动视图,因为我的类从UIScrollView延伸。

所以,请帮我

+0

我喜@Limon我不关心标签,我需要滚动视图点击。 – Houssam

+0

你的意思是_clickable_?在iOS中没有像_click_这样的事件,我们正在这里使用_touches_和_gestures_。 – holex

+0

hi @holex我喜欢触摸,所以当我触摸scrollview什么也没有发生 – Houssam

回答

0

你必须通过以下方式来继承UIScrollView。然后使用您的自定义滚动视图(即TouchScrollView),而不是常规UIScrollView

TouchScrollView.h:

#import <Foundation/Foundation.h> 
@import UIKit; 

@interface TouchScrollView : UIScrollView 

@end 

TouchScrollView.m:

#import "TouchScrollView.h" 

@implementation TouchScrollView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // Respond to touch events here 
    // ... 


    [self.nextResponder touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.nextResponder touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.nextResponder touchesEnded:touches withEvent:event]; 
} 

@end 

或者,如果你想在斯威夫特这样做,因为它是时下的时尚:

TouchScrollView.swift

import Foundation 
import UIKit 

class TouchScrollView: UIScrollView { 
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { 
     // Respond to touch events here 
     // ... 
     nextResponder().touchesBegan(touches, withEvent: event) 
    } 

    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) { 
     nextResponder().touchesMoved(touches, withEvent: event) 
    } 

    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) { 
     nextResponder().touchesEnded(touches, withEvent: event) 
    } 
} 
+0

我尝试它,但它没有工作 – Houssam

+0

如果您添加'NSLog(“HIT”)'touchesBegan方法,当你触摸滚动视图时你没有输出? – mprivat

+0

是没有输出:( – Houssam