自定义UIPageControl视图,用“X页的X”替换点。 [英] Custom UIPageControl view, replacing dots with "Page X of Y"

查看:94
本文介绍了自定义UIPageControl视图,用“X页的X”替换点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来替换UIPageControl的标题为第X页的Y,因为我可能会有> 50个项目。我只是熟悉Cocoa,我想知道什么是最好的方法做到这一点。我可以继承UIPageControl吗?我应该使用带标签的自定义视图吗?或其他?

I'm trying to find a way to replace the dots of a UIPageControl with a caption that reads "Page X of Y" since I'll likely have >50 items. I'm just becoming familiar with Cocoa, and I was wondering what is the best way to do this. Can I subclass UIPageControl? Should I use a custom view with labels? Or something else?

编辑

=http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html =nofollow noreferrer>这个代码由Matt Gallagher实现我的分页视图。它也更有效,因为它重用了两个视图,而不是一次创建它们,如在PageControl的Apple示例代码中所做的。

I ended up using this code by Matt Gallagher to achieve my paging view. It's also more efficient since it re-uses two views, instead of creating them all at once, as is done in the Apple sample code for PageControl.

推荐答案

编辑:

这个答案已经收到几个upvote。谢谢,但是对于所有来到这里,请意识到,UIPageControl是非常容易实现自己!更多的是,看看我的回答和示例代码下面的一些想法,但你真的不应该在生产代码中使用它。滚动你自己的UIControl的子类,如果你需要修改外观&感觉到UIPageControl。

This answer has received several upvotes. Thank you but for all who come here please realize that the UIPageControl is very easy to implement yourself! More to the point, take a look at my response and sample code below for some ideas but you really shouldn't use it in production code. Roll your own subclass of UIControl if you need to modify the look & feel of a UIPageControl.

原始答案:

UIPageControl没有做任何花哨。对于大多数视图,视图本身或者使用drawRect:或者它添加子视图和绘制自己。通过重写drawRect:而不是调用[super drawRect:]你确保如果实现是drawRect: - 基于你将有你自己的绘图代码调用。在UIPageControl的情况下,它依赖于已添加到UIPageControl的子视图。你真的不需要知道什么样的,但也不应该关心。

The UIPageControl isn't doing anything fancy. For most views the view itself either does all its drawing in drawRect: or it adds subviews and those draw themselves. By overriding drawRect: and not calling [super drawRect:] you ensure that if the implementation is drawRect:-based you'll have your own drawing code called. In the case of UIPageControl, it relies on subviews that have been added to UIPageControl. You don't really need to know what kind though nor should you care. It's Apple's implementation and it's subject to change.

但是因为你知道它必须是drawRect或者基于子视图,所以你可以简单地删除子视图UIPageControl,然后你的drawRect:override将按照你的期望工作(主要是你必须做一些额外的工作,以确保你在正确的时间重绘)。

But because you know it has to be either drawRect: or subview-based, you can get away with simply removing the subviews of the UIPageControl and then your drawRect: override will work as you'd expect (mostly, you have to do a little bit of extra work to make sure you redraw at the right times).

这里有一个例子演示如何制作自己的自定义UIPageControl。精明的读者会注意到,一旦你遇到麻烦,这样做,你可能刚刚创建了自己的页面控件作为UIControl的子类,只是实现了UIPageControl API。

Here's an example that shows how to make your own custom UIPageControl. The astute reader will notice that once you've gone to the trouble to do it this way you might as well have just created your own page control as a subclass of UIControl and simply implemented the UIPageControl API.

//
//  RedGreyPageControl.m
//

@interface RedGreyPageControl : UIPageControl {
    NSArray                     *originalSubviews;
}

@end


@implementation RedGreyPageControl


// This assumes you're creating the control from a nib.  Depending on your
// usage you might do this step in initWithFrame:
- (void) awakeFromNib {
    // retain original subviews in case apple's implementation
    // relies on the retain count being maintained by the view's
    // presence in its superview.
    originalSubviews = [[NSArray alloc] initWithArray: self.subviews];

    for ( UIView *view in self.subviews ) [view removeFromSuperview];

    // make sure the view is redrawn not scaled when the device is rotated
    self.contentMode = UIViewContentModeRedraw;
}


- (void) dealloc {
    [originalSubviews release];
    [super dealloc];
}


- (void) drawRect:(CGRect) iRect {
    UIImage                 *grey, *image, *red;
    int                     i;
    CGRect                  rect;

    const CGFloat           kSpacing = 10.0;

    iRect = self.bounds;

    if ( self.opaque ) {
        [self.backgroundColor set];
        UIRectFill( iRect );
    }

    if ( self.hidesForSinglePage && self.numberOfPages == 1 ) return;

    red = [UIImage imageNamed: @"circle_graphic_red.png"];
    grey = [UIImage imageNamed: @"circle_graphic_grey.png"];

    rect.size.height = red.size.height;
    rect.size.width = self.numberOfPages * red.size.width + ( self.numberOfPages - 1 ) * kSpacing;
    rect.origin.x = floorf( ( iRect.size.width - rect.size.width ) / 2.0 );
    rect.origin.y = floorf( ( iRect.size.height - rect.size.height ) / 2.0 );
    rect.size.width = red.size.width;

    for ( i = 0; i < self.numberOfPages; ++i ) {
        image = i == self.currentPage ? red : grey;

        [image drawInRect: rect];

        rect.origin.x += red.size.width + kSpacing;
    }
}


// you must override the setCurrentPage: and setNumberOfPages:
// methods to ensure that your control is redrawn when its values change
- (void) setCurrentPage:(NSInteger) iPage {
    [super setCurrentPage: iPage];
    [self setNeedsDisplay];
}


- (void) setNumberOfPages:(NSInteger) iPages {
    [super setNumberOfPages: iPages];
    [self setNeedsDisplay];
}


@end

这篇关于自定义UIPageControl视图,用“X页的X”替换点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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