2014-06-26 146 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