如何在 iOS 中使用 UIImageview 添加 UIScrollview 的页面控件

Posted

技术标签:

【中文标题】如何在 iOS 中使用 UIImageview 添加 UIScrollview 的页面控件【英文标题】:How can I add the Page control for UIScrollview with UIImageview in iOS 【发布时间】:2012-07-31 05:21:41 【问题描述】:

如何在 ios 中为 UIScrollView 添加 页面控件UIImageView。如果我选择了某些页面控制项目符号意味着我想移动选定的图像。如果我移动图像意味着我的页面控件项目符号也想在UIScrollView 中移动。

 - (void)viewDidLoad

    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

   // self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
// Do any additional setup after loading the view, typically from a nib.




- (void)loadView 


    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(1400, 100);

    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti01.png"]];
    tempImageView2.frame=CGRectMake(10, 60, 200, 200);
    [scrollView addSubview:tempImageView2];


    UIImageView *tempImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti02.png"]];
    tempImageView3.frame=CGRectMake(240, 60, 200, 200);
    [scrollView addSubview:tempImageView3];

    UIImageView *tempImageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti03.png"]];
    tempImageView4.frame=CGRectMake(470, 60, 200, 200);
    [scrollView addSubview:tempImageView4];

    UIImageView *tempImageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti04.png"]];
    tempImageView5.frame=CGRectMake(700, 60, 200, 200);
    [scrollView addSubview:tempImageView5];


    UIImageView *tempImageView6 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti05.png"]];
    tempImageView6.frame=CGRectMake(930, 60, 200, 200);
    [scrollView addSubview:tempImageView6];

    UIImageView *tempImageView7 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti06.png"]];
    tempImageView7.frame=CGRectMake(1160, 60, 200, 200);
    [scrollView addSubview:tempImageView7];



    self.view=scrollView;
    [scrollView addSubview:tempImageView2];
    [scrollView addSubview:tempImageView3];
    [scrollView addSubview:tempImageView4];
    [scrollView addSubview:tempImageView5];
    [scrollView addSubview:tempImageView6];
    [scrollView addSubview:tempImageView7];

    scrollView.userInteractionEnabled = YES;
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(22, 100, 1800, 500);
   // [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];    
    [scrollView addSubview:btn];





- (IBAction)buttonTest:(id)sender 
    MSDescriptionpage *aSecondPageController = [[MSDescriptionpage  alloc] initWithNibName:@"MSDescriptionpage" bundle:nil];        
    [self.navigationController pushViewController:aSecondPageController animated:YES];        
    [aSecondPageController release];


【问题讨论】:

raywenderlich.com/10518/… 这是给 iphone 的。但我想为 ipad 做。 嗨 nikhil 了解这段代码如何使用 uibutton 将图像动态传递到下一页... 【参考方案1】:

我必须这样做,我所做的是我使用了两个视图控制器(用于页面滑动的页面视图控制器)和带有 UIImage 视图的主视图控制器,您可以在此添加带有图像的滚动视图.. 这些在页面 VC 里面确实加载了

  [pageController setViewControllers:viewControllers  
                             direction:UIPageViewControllerNavigationDirectionReverse  
                              animated:NO 
                            completion:nil];

    [self addChildViewController:pageController];
    [[self view] addSubview:[pageController view]];
    for (UIGestureRecognizer *gR in pageController.gestureRecognizers) 
        gR.delegate = self;
    
    [pageController didMoveToParentViewController:self];

这些调用另一个vc

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController

    for (UIGestureRecognizer *recognizer in pageController.gestureRecognizers) 
        if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) 
            recognizer.enabled = NO;
        
    
    NSUInteger index = [self indexOfViewController:
                        (MainViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) 
        return nil;
    

    index--;
    return [self viewControllerAtIndex:index];

【讨论】:

好的,如果这有帮助你可以投票..如果你的问题解决了,你可以检查我的答案是否正确..

以上是关于如何在 iOS 中使用 UIImageview 添加 UIScrollview 的页面控件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中保存 UIImageView 数据

如何使用 UIImageView 在 iOS4 中显示高分辨率图像

我如何在 Swift iOS 中检测 UIImageView 中的更改图像

如何在 iOS 上的 UIScrollView 中正确替换 UIImageView 中的图像

如何在ios目标c的UIImageView中添加UIView

iOS:使用 UIViewContentModeScaleAspectFill 时如何对齐(左/右) UIImageView?