嗨,我的应用程序需要在水平列表中以中心焦点显示图像列表。使用UIScrollView
and可以正常工作PagingEnable=Yes
;现在我无法滚动显示在UIScrollView
边界之外的内容。
如何将 UiscrollView 滚动到边界之外。
嗨,我的应用程序需要在水平列表中以中心焦点显示图像列表。使用UIScrollView
and可以正常工作PagingEnable=Yes
;现在我无法滚动显示在UIScrollView
边界之外的内容。
如何将 UiscrollView 滚动到边界之外。
假设您有一个与图像高度相同的容器视图,但其大小使所有图像都可以放入其中。每个图像的帧都会有一个“x”偏移量,将其放在图像的末尾。现在假设您的容器中有 3 张图片,每张 320 宽,总共 960 张。
鉴于此,您必须设置 scrollView.contentSize = CGSizeMake(960, 320);
在这里,我给出了一些关于如何水平管理滚动视图的教程,如下所示。
.h 文件包含
@property (nonatomic, strong) NSArray *arForImages; // array for storing images URL
@property (nonatomic, strong) IBOutlet UIScrollView *scrPage; // scroll view used as PageCtr
@property (nonatomic, strong) IBOutlet HJManagedImageV *imgV; // Image to display selected
@property (nonatomic, strong) IBOutlet UILabel *lblName; // label to display selected's title
@property (nonatomic, readwrite) NSUInteger currentPage; // to mantain current page
.m 文件包含
- (void)viewDidLoad
{
[super viewDidLoad];
self.arForImages = [NSArray arrayWithObjects:@"http://www.apple.com/in/iphone/iphone-3gs/images/iphone-phone-20100607.jpg",@"http://www.apple.com/in/iphone/iphone-3gs/images/iphone-ibooks-20100607.jpg",@"http://www.blogcdn.com/www.engadget.com/media/2007/01/apple-iphone-official-1.jpg",@"http://www.dailytechpost.com/wp-content/uploads/2011/08/iphone-3G1.jpg",@"http://www.iunlockiphone3g.com/wp-content/uploads/2011/01/Apple-Iphone-India-Launch1.jpg",@"http://st2.gsmarena.com/vv/pics/apple/apple-iphone-3g-01.jpg",@"http://resources.infosecinstitute.com/wp-content/uploads/iphone.jpg",@"http://bindapple.com/wp-content/uploads/2011/01/can-iphones-get-viruses1.jpg",@"http://www.coolpctips.com/wp-content/uploads/2011/08/iphone-mailbox.jpg",@"http://www.everymac.com/images/other_images/iphone-iphone-3g-comparison.jpg",@"http://iphone4issues.org/wp-content/uploads/2011/07/Iphone-4-Issues1.jpg",nil];
[self setupImages];
}
滚动视图的分页
#define kXDistance 5
#define kYDistance 5
#define kThumbWidth 58
#define kThumbHeight 58
#define kTagForImageView 10000
- (void)setupImages {
// This will remove all subview from your scrollView
[[self.scrPage subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
// set the content size of your scrollView to number of object that you have in your array
self.scrPage.contentSize=CGSizeMake((self.arForImages.count+4)*63, 68);
// we will start from two in this tutorial, you can start from one also.
NSUInteger i=2; // images starting from location 2
for (NSString *str in self.arForImages) {
// buttons will have images within it & we will add buttons to scrollView
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
NSUInteger xVal=(i+1)*kXDistance;
xVal = xVal + (i*kThumbWidth);
[btn setFrame:CGRectMake(xVal, kYDistance, kThumbWidth, kThumbHeight)];
btn.tag=i-1; // index tagging strating from 1
HJManagedImageV *imgV=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, kThumbWidth, kThumbHeight)];
imgV.tag=kTagForImageView;
imgV.url=[NSURL URLWithString:[self.arForImages objectAtIndex:i-2]];
imgV.clipsToBounds=YES;
imgV.layer.borderColor = [[UIColor blackColor] CGColor];
imgV.layer.borderWidth = 1;
imgV.layer.cornerRadius = 3;
// here GlobalCacheManager is an object declared in AppDel which is accessed via a Constant
[GlobalCacheManager manage:imgV];
[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[btn addSubview:imgV];
[self.scrPage addSubview:btn];
i++;
}
self.lblName.text=[[self.arForImages objectAtIndex:0] lastPathComponent];
self.imgV.url=[NSURL URLWithString:[self.arForImages objectAtIndex:0]];
[GlobalCacheManager manage:self.imgV];
self.currentPage = 0;
}
- (void)btnTapped:(UIButton*)btn {
self.scrPage.delegate=nil;
CGRect rectToScroll = btn.frame;
rectToScroll=CGRectMake(rectToScroll.origin.x-63-63, kYDistance, 320, 68);
[self.scrPage scrollRectToVisible:rectToScroll animated:YES];
self.lblName.text=[[self.arForImages objectAtIndex:btn.tag-1] lastPathComponent];
self.imgV.url=[NSURL URLWithString:[self.arForImages objectAtIndex:btn.tag-1]];
[GlobalCacheManager manage:self.imgV];
[self.scrPage performSelector:@selector(setDelegate:) withObject:self afterDelay:0.3];
}
希望这可能对您有所帮助。