Ios 使用水平滚动在图像视图上加载图像
我正在通过字符串将图像加载到Ios 使用水平滚动在图像视图上加载图像,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我正在通过字符串将图像加载到UIImageView上,我希望通过水平滚动查看UIImageView上的图像。 在我的xib文件中,我有一个滚动视图,上面有一个图像视图。我正在使用此代码,但我的页面没有滚动,只加载了字符串中的第一个图像。请任何人帮助 代码: -(void)viewDidLoad { 计数=1; //imagesName=[[NSMutableArray alloc]initWithObjects:@“election_band_base.jpg”,“ElectionBoxAPPA
UIImageView
上,我希望通过水平滚动查看UIImageView
上的图像。
在我的xib文件中,我有一个滚动视图,上面有一个图像视图。我正在使用此代码,但我的页面没有滚动,只加载了字符串中的第一个图像。请任何人帮助
代码:
-(void)viewDidLoad
{
计数=1;
//imagesName=[[NSMutableArray alloc]initWithObjects:@“election_band_base.jpg”,“ElectionBoxAPPA Hindi(1.jpg)”,@“photos.png”,“business.png”,“health.png”,nil];
imagesName1=[NSString stringWithFormat:@“election_band_base.jpg”,“ElectionBoxAPPA Hindi(1.jpg)”,@“photos.png”,“business.png”,“health.png”,nil];
[imagesName添加对象:imagesName 1];
items=[[NSMutableArray alloc]init];
//[_imageView1.image setImage=imagesName1];
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
//_imageView1.image=[UIImage animatedImageWithImages:imagesName持续时间:0];
_imageView1.image=[UIImage ImageName:imagesName1];
[自动加载滚动视图];
}
-(void)加载滚动视图
{
scrollView.contentSize=CGSizeMake(0,scrollView.frame.size.height);
NSMutableArray*控制器=[[NSMutableArray alloc]init];
for(无符号i=0;i<[imagesName计数];i++){
[控制器添加对象:[NSNull];
}
self.viewControllers=控制器;
计数=1;
//页面是滚动视图的宽度
scrollView.PaginEnabled=是;
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*[imagesName count],scrollView.frame.size.height);
//scrollView.contentSize=CGSizeMake(900,80);
scrollView.showsHorizontalScrollIndicator=是;
scrollView.showsVerticalScrollIndicator=是;
scrollView.scrollsToTop=否;
scrollView.delegate=self;
pageControl.numberOfPages=[imagesName计数];
pageControl.currentPage=0;
//页面是按需创建的
//加载可见页面
//在任意一侧加载页面,以避免用户开始滚动时闪烁
[带页面的自加载滚动视图:0];
[带第1页的自动加载滚动视图];
}
-(void)加载滚动视图带页面:(int)页面{
如果(第<0页)返回;
如果(第>=[图像名称计数])
返回;
//如有必要,请替换占位符
控制器=[viewControllers objectAtIndex:page];
如果((NSNull*)控制器==[NSNull]){
NSString*deviceType=[UIDevice currentDevice]。型号;
如果([deviceType IsequalString:@“iPhone”])
{
控制器=[[MyViewController alloc]initWithNibName:@“MyViewController”捆绑包:nil];
}
否则{
控制器=[[MyViewController alloc]initWithNibName:@“MyViewController_ipad”捆绑包:nil];
}
[控制器初始化与页码:第页];
[控制器设置arrData:imagesName];
[viewControllers replaceObjectAtIndex:page with Object:controller];
}
//将控制器视图添加到滚动视图
if(nil==controller.view.superview){
CGRect frame=scrollView.frame;
frame.origin.x=frame.size.width*页;
frame.origin.y=0;
controller.view.frame=frame;
[scrollView添加子视图:controller.view];
}
}
-(void)unloadScrollViewWithPage:(int)第页{
如果(第<0页)返回;
如果(第>=[图像名称计数])返回;
控制器=[viewControllers objectAtIndex:page];
如果((NSNull*)控制器!=[NSNull]){
if(nil!=controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull]];
}
}
-(无效)scrollViewDidScroll:(UIScrollView*)发送方{
//我们不希望UIPageControl和中的滚动委托之间出现“反馈循环”
//由用户点击页面控件生成的滚动事件触发的更新
//委托方法。当使用页控件时,我们使用布尔值禁用委托逻辑。
如果(已使用页面控制){
//不执行任何操作-滚动是从页面控件启动的,而不是用户拖动的
返回;
}
//当上一页/下一页显示超过50%时,切换指示器
CGFloat pageWidth=scrollView.frame.size.width;
int page=floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
pageControl.currentPage=页面;
//NSLog(@“当前页%d”,第页);
//加载可见页面及其两侧的页面(以避免用户开始滚动时闪烁)
[self-unloadScrollViewWithPage:page-2];
[带页面的自加载滚动视图:第1页];
[带页面的自加载滚动视图:第页];
[self-loadScrollViewWithPage:page+1];
[self-UnloadScrollViewWith page:page+2];
计数=页码+1;
//一种可能的优化方法是卸载不再可见的视图+控制器
}
//在滚动拖动开始时,重置滚动源自UIPageControl时使用的布尔值
-(无效)ScrollViewWillBeginDraging:(UIScrollView*)scrollViewLoc
{
CGFloat pageWidth=scrollViewLoc.frame.size。
-(void)viewDidLoad
{
count=1;
// imagesName = [[NSMutableArray alloc]initWithObjects :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil];
imagesName1 = [NSString stringWithFormat :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil];
[imagesName addObject:imagesName1];
items = [[NSMutableArray alloc]init];
// [_imageView1.image setImage=imagesName1];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// _imageView1.image=[UIImage animatedImageWithImages:imagesName duration:0];
_imageView1.image=[UIImage imageNamed:imagesName1 ];
[self loadScrollView];
}
-(void)loadScrollView
{
scrollView.contentSize = CGSizeMake(0, scrollView.frame.size.height);
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [imagesName count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
count=1;
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imagesName count], scrollView.frame.size.height);
//scrollView.contentSize = CGSizeMake(900,80);
scrollView.showsHorizontalScrollIndicator =YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = [imagesName count];
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [imagesName count])
return;
// replace the placeholder if necessary
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"])
{
controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
}
else{
controller = [[MyViewController alloc] initWithNibName:@"MyViewController_ipad" bundle:nil];
}
[controller initWithPageNumber:page];
[controller setArrData:imagesName];
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)unloadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [imagesName count]) return;
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
if (nil != controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// NSLog(@"current page %d",page);
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self unloadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self unloadScrollViewWithPage:page + 2];
count=page+1;
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc
{
CGFloat pageWidth = scrollViewLoc.frame.size.width;
CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview];
int page = floor((scrollViewLoc.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
- (void)makeFriendsScrollView{
int friendsCount = 10;
float xPos = 10;
for (int i = 0; i < friendsCount; i++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos,25 , 54, 54)];
imgView.image = [UIImage imageNamed:@"user_default.png"];
[imgView.layer setCornerRadius:27.0];
imgView.clipsToBounds = YES;
[scrollViewFriends addSubview:imgView];
xPos = xPos + 64;
}
scrollViewFriends.contentSize = CGSizeMake(friendsCount*65, 90);
[scrollViewFriends.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[scrollViewFriends.layer setBorderWidth:0.5f];
}
self.arraname=[NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg", nil];
// int friendsCount = 10;
float xPos = 160;
float x1=0;
float y=60;
for (int i = 0; i < [self.arraname count]; i++)
{
x1=xPos+(260*i);
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x1, y, 54, 54)];
_imgView.image = [UIImage imageNamed:[self.arraname objectAtIndex:i]];
[_imgView.layer setCornerRadius:27.0];
_imgView.clipsToBounds = YES;
[self.scroll addSubview:_imgView];
}
NSLog(@"%f",x1);
self.scroll.contentSize=CGSizeMake(x1+200, 0);
self.scroll.showsHorizontalScrollIndicator = YES;
self.scroll.showsVerticalScrollIndicator=NO;
self.scroll.pagingEnabled = YES;
ImageArray=[[NSMutableArray alloc]init];
for (int i=0; i<19; i++) {
NSString *imgtext=[[NSString alloc]initWithFormat:@"img%d",i+1];
[ImageArray addObject:imgtext];
}
for (int i = 0; i < ImageArray.count; i++) {
CGRect frame;
frame.origin.x = self.scrollview.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollview.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[ImageArray objectAtIndex:i]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setFrame:CGRectMake(0, 0, frame.size.width,frame.size.height )];
[subview addSubview:imageView];
[self.scrollview addSubview:subview];
}
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * ImageArray.count, self.scrollview.frame.size.height);
self.scrollview.contentOffset=CGPointMake (self.scrollview.frame.size.width, 0);