以编程方式在 UIView 中添加 UIScrollView [英] Add UIScrollView in UIView programmatically

查看:27
本文介绍了以编程方式在 UIView 中添加 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如何在 UIView 中添加 UIScrollView,以便 UIScrollView 可以正常工作.

I have a question how to add UIScrollView in UIView, so that the UIScrollView could work properly.

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
    CGFloat xOrigin = i * container.frame.size.width/2;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:awesomeView];
    [awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];

以上代码来自教程:http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/但它对我不起作用.

Above code is from tutorial: http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/ But it doesn't work for me.

如果您有一个问题,您已经正确设置了滚动视图但它不起作用,请确保您没有与另一个 UIView 覆盖您的滚动视图.那是我的问题.解决了!

If you have a problem that you have set up a scrollview properly but it's not working, make sure that you are not overlaying with another UIView your scrollview. That was my problem. Solved!

推荐答案

您可以使用以下代码在您的View上添加 UIScrollView :-

You can use below code to add UIScrollView on yourView :-

第 1 步:

Delegate "UIScrollViewDelegate" to your ViewController.h

for example:
  @interface yourViewController:UIViewController<UIScrollViewDelegate> 

第 2 步:

//create you UIScrollView
UIScrollView  *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)]; 
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)

第 3 步:

你想在 ViewController.m 中实现 scrollView Delegates

you want to implement the scrollView Delegates in ViewController.m

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
   return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   NSLog(@"Did end decelerating");
   //do your code here
}   
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   NSLog(@"Did scroll");
   //do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"Did end dragging");
   //do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"Did begin decelerating");
   //do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
   NSLog(@"Did begin dragging");
   //do your code here
}

这篇关于以编程方式在 UIView 中添加 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆