在ios的视图上的水平滚动

时间:2014-05-08 08:43:24

标签: ios

如何实现滚动视图以水平滚动从NSDictionary加载的视图页面。目前我正在使用swipegesture,但这有点慢。 我应该用什么代码来实现水平滚动?

我正在使用此代码:

-(void)DownLoadData:(NSString *)indexSelect
{


    {
        [[SharedUtilities getInstance]AddActivityIndicatorViewOnMainThread:self.view];
    }

    self._parserForNewsDetail = [afaqsParser getInstance];
    [[afaqsParser getInstance] setCacheNeed:TRUE];
    [self._parserForNewsDetail   parseWithUrl:[_arrUrlLinks objectAtIndex:[indexSelect integerValue]] UrlTypefor:nil];
    NSDictionary *resultDic;

    resultDic = [[[self._parserForNewsDetail getLinkAndIdDic] valueForKey:@"items"]objectAtIndex:0];


    NSLog(@"Detail Dic = %@",[resultDic description]);
    if (resultDic== NULL || resultDic ==nil)
    {
        //Check internet here
        [[SharedUtilities getInstance]RemoveActivityIndicatorView];
        [SharedUtilities ShowAlert:@"No Data Found" title:nil withView:self.view];
        return;
    }

    [self performSelectorOnMainThread:@selector(SetValuesInUserInterface:)  withObject: resultDic waitUntilDone:NO];



    [[SharedUtilities getInstance]RemoveActivityIndicatorView];
}

-(void)SetValuesInUserInterface:(NSDictionary *)Dic
{



    self._imageView1.layer.cornerRadius = 4;
    self._imageView1.clipsToBounds = YES;


    self._imageView1.tag = 999;

    NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    self._imageView1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]];




    NSArray *subviewsArr = [self.view subviews];
    for (int i=0; i<[subviewsArr count]; i++) {
        if ([[subviewsArr objectAtIndex:i] isKindOfClass:[ImageDownLoader class]]) {
            [[subviewsArr objectAtIndex:i] removeFromSuperview];


        }
    }


    if ([[Dic valueForKey:@"image"] isEqual:@""])
    {
        // strg=[NSString stringWithFormat:@"%@,  ",[Dic valueForKey:@"image"]];
        _imageView1.hidden=YES;
        // _txtView.frame=CGRectMake(4.0f,95.0f,310.0f,100.0f );
        _txtView.frame=CGRectMake(4.0f,95.0f,_txtView.frame.size.width,_txtView.frame.size.height );


        NSLog(@"NO IMAGE");
    }
    else{
        _imageView1.hidden=NO;
        _imageView1.frame=CGRectMake(4.0f,95.0f,310.0f,180.0f );
      _txtView.frame=CGRectMake(4.0f,316.0f,_txtView.frame.size.width,_txtView.frame.size.height );

        NSLog(@"IMAGE VISIBLE");
    }



    self._scrollView.scrollEnabled = YES;
    self._scrollView.showsVerticalScrollIndicator = YES;
    self._scrollView.showsHorizontalScrollIndicator = YES;
    self._header.font = [UIFont fontWithName:@"HelveticaNeue-MediumCond" size:18];
    [self._header setText: [Dic valueForKey:@"header"]];
    self._header.textColor=[UIColor blackColor];
    [self._publicationDate setText:[Dic valueForKey:@"PUB_DATE"]];


    [self._kicker setText:[Dic valueForKey:@"kicker"]];
    [self._txtView setText:[Dic valueForKey:@"ARTICLE_BODY"]];

    NSString *writer;
    if ([[Dic valueForKey:@"AUTHOR"] length]>2)
    {
        writer=[NSString stringWithFormat:@"%@,  ",[Dic valueForKey:@"AUTHOR"]];
    }
    else
    {
        writer=@"";
    }
    NSString *city;
    if ([[Dic valueForKey:@"REPORTING_CITY"] length]>2)
    {
        city=[NSString stringWithFormat:@",  %@",[Dic valueForKey:@"REPORTING_CITY"]];
    }
    else
    {
        city=@"";
    }


    NSString *str = [NSString stringWithFormat:@"%@ee%@", writer,city];
    //[cell._Writer setText: [tempDic valueForKey:@"writer"]];
    [self._Writer setText:str];
    [_txtView sizeToFit]; //added
    [_txtView layoutIfNeeded]; //added

    CGRect frame = self._txtView.frame;
    self._txtView.frame = frame;
    [_txtView setScrollEnabled:NO];
    self._scrollView.contentSize = CGSizeMake(320,440+frame.size.height);

    _titleLabel.frame= CGRectMake(0, self._scrollView.contentSize.height-119, [[UIScreen mainScreen] bounds].size.width, 40);
    _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13.5];
    _titleLabel.numberOfLines=2;

    [self._scrollView addSubview:_titleLabel];

}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}






- (void)viewDidLoad
{

    [super viewDidLoad];
    _titleLabel = [[UILabel alloc] init];


    lblTitle.font = [UIFont fontWithName:@"HelveticaNeue-MediumCond" size:20]; 

    lblTitle.text=_strTitle;
    NSLog(@"arrUrls %d",[_arrUrlLinks count]);
    NSLog(@"strCurrentNewsSelect %@",_strCurrentNewsSelect);


    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"DISABLEGESTURE"
     object:self];

    count=[_strCurrentNewsSelect integerValue];
    [self performSelectorInBackground:@selector(DownLoadData:) withObject:_strCurrentNewsSelect];
    if([_strCurrentNewsSelect isEqualToString:@"0"])
    {
        btnPreviousNews.userInteractionEnabled=FALSE;
    }
    else{

    }
    _lblNewsCount.font = [UIFont fontWithName:@"HelveticaNeue-MediumCond" size:16];
    _lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",[_strCurrentNewsSelect integerValue]+1,[_arrUrlLinks count]];
    // Do any additional setup after loading the view from its nib.
    UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(280, 340, 40, 40)];
    [shareBtn addTarget:self action:@selector(Share:) forControlEvents:UIControlEventTouchUpInside];
    [shareBtn setBackgroundImage:[UIImage imageNamed:@"share1.png"]  forState:UIControlStateNormal];
    //    [self.view addSubview:shareBtn];
    if([_strCurrentNewsSelect isEqualToString:@"0"])
    {
        btnPreviousNews.userInteractionEnabled=FALSE;
        [btnPreviousNews setImage:[UIImage imageNamed:@"arrow2_prev.png"] forState:UIControlStateNormal];
    }

    if([_strCurrentNewsSelect isEqualToString:[NSString stringWithFormat:@"%d",[_arrUrlLinks count]-1]])
    {
        btnNextNews.userInteractionEnabled=FALSE;
        [btnNextNews setImage:[UIImage imageNamed:@"arrow2_next.png"] forState:UIControlStateNormal];
    }

    UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [rightRecognizer setNumberOfTouchesRequired:1];

    //add the your gestureRecognizer , where to detect the touch..
    [_scrollView addGestureRecognizer:rightRecognizer];

    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];

    [_scrollView addGestureRecognizer:leftRecognizer];

}

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
      [self btnPreviousClick];


}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSLog(@"leftSwipeHandle");
     [self btnNextClick];

}


-(IBAction)Share:(UIButton *)sender
{
    NSLog(@"SHare called =%d",sender.tag);
    // NSDictionary *tempDic = [[self._resultDic valueForKey:@"items"] objectAtIndex:sender.tag];

    [[SharedUtilities getInstance] set_LinkForSharing:[[[[self._parserForNewsDetail getLinkAndIdDic] valueForKey:@"items"]objectAtIndex:0] valueForKey:@"image"]];
    [[SharedUtilities getInstance]set_headerForSharing:[[[[self._parserForNewsDetail getLinkAndIdDic] valueForKey:@"items"]objectAtIndex:0] valueForKey:@"header"]];
    [[SharedUtilities getInstance]set_viewController:self];
    [[SharedUtilities getInstance]Share];
}


-(IBAction)btnBackPress:(id)sender;
{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"ENABLEGESTURE"
     object:self];
    [self.navigationController popViewControllerAnimated:YES];
    lblTitle.text=_strTitle;
}
-(IBAction)btnNextClick
{
    btnPreviousNews.userInteractionEnabled=TRUE;
    if(count!=[_arrUrlLinks count] -1)
    {
        if(count==[_arrUrlLinks count]-2)
        {
            btnNextNews.userInteractionEnabled=FALSE;
            [btnNextNews setImage:[UIImage imageNamed:@"arrow2_next.png"] forState:UIControlStateNormal];
        }
        [btnPreviousNews setImage:[UIImage imageNamed:@"arrow1_prev.png"] forState:UIControlStateNormal];
        count=count +1;
        _lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",count+1,[_arrUrlLinks count]];
        NSLog(@"next %d",count);
        [self performSelectorInBackground:@selector(DownLoadData:) withObject:[NSString stringWithFormat:@"%d",count]];
    }
    else{
        btnNextNews.userInteractionEnabled=FALSE;
    }
}
-(IBAction)btnPreviousClick
{
    btnNextNews.userInteractionEnabled=TRUE;
    if(count==0)
    {
        btnPreviousNews.userInteractionEnabled=FALSE;
    }
    else{
        if(count==1)
        {
            [btnPreviousNews setImage:[UIImage imageNamed:@"arrow2_prev.png"] forState:UIControlStateNormal];
            btnPreviousNews.userInteractionEnabled=FALSE;
        }
        [btnNextNews setImage:[UIImage imageNamed:@"arrow1_next.png"] forState:UIControlStateNormal];
        count=count-1;

        _lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",count+1,[_arrUrlLinks count]];
        NSLog(@"previous %d",count);
        [self performSelectorInBackground:@selector(DownLoadData:) withObject:[NSString stringWithFormat:@"%d",count]];
    }
}





}

3 个答案:

答案 0 :(得分:0)

看看UIScrollView。说实话,这个问题让你看起来对这个问题做了很少的研究。您能告诉我们更多关于您目前拥有的以及您迄今为止所做的工作吗?

答案 1 :(得分:0)

UIScrollView Scroll取决于ContentSize。所以你必须设置ContentSize。

用于Horizinatal滚动

    [scroll setContentSize:CGSizeMake(1500, scroll.frame.size.height)];

答案 2 :(得分:0)

您是否尝试过以下方法,

    UIScrollView * _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 10.0, 320.0, 280.0)];

    _scrollView.backgroundColor = [UIColor clearColor];

    _scrollView.pagingEnabled = YES;

    _scrollView.showsHorizontalScrollIndicator = NO;

    _scrollView.showsVerticalScrollIndicator = NO;

    _scrollView.scrollsToTop = NO;

    _scrollView.delegate = self;

    [self addSubview:_scrollView];

    float width = 0.0;

    for (int i = 0; i < pageCount; i++)
    {
          UIView * yourView = [[UIView alloc] initWithFrame:CGRectMake((i * 320.0) + 20.0, 0.0, 280.0, 280.0);

          [_scrollView addSubview:yourView];

          width = yourView.frame.size.width + yourView.frame.origin.x + 20.0;
    }

    [_scrollView setContentSize:CGSizeMake(width, _scrollView.frame.size.height)];