Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 自定义UIPageControl视图,将点替换为“点”;第X页,共Y页“;_Iphone_Cocoa Touch_Uikit - Fatal编程技术网

Iphone 自定义UIPageControl视图,将点替换为“点”;第X页,共Y页“;

Iphone 自定义UIPageControl视图,将点替换为“点”;第X页,共Y页“;,iphone,cocoa-touch,uikit,Iphone,Cocoa Touch,Uikit,我正试图找到一种方法,将UIPageControl中的点替换为标题“第X页,共Y页”,因为我可能会有>50个项目。我刚刚开始熟悉可可,我想知道做这件事的最好方法是什么。我可以子类UIPageControl吗?我应该使用带有标签的自定义视图吗?还是别的什么 编辑: 我最终使用Matt Gallagher的代码来实现我的分页视图。它还更高效,因为它重新使用了两个视图,而不是像PageControl的Apple示例代码那样一次创建所有视图。我认为编写自定义页面控件不会违反人机界面准则。但我认为显示>5

我正试图找到一种方法,将UIPageControl中的点替换为标题“第X页,共Y页”,因为我可能会有>50个项目。我刚刚开始熟悉可可,我想知道做这件事的最好方法是什么。我可以子类UIPageControl吗?我应该使用带有标签的自定义视图吗?还是别的什么

编辑:


我最终使用Matt Gallagher的代码来实现我的分页视图。它还更高效,因为它重新使用了两个视图,而不是像PageControl的Apple示例代码那样一次创建所有视图。

我认为编写自定义页面控件不会违反人机界面准则。但我认为显示>50个点会更好;)

我认为双向都可以接受

您可以创建UIPageControl的自定义子类,并覆盖默认的点绘制行为。您必须小心,不要忽略您确实需要的其他行为


或者,由于UIPageControl类的重新实现并不复杂,编写一个完整的新类可以确保在重写点绘制逻辑时不会重写所需的行为,因此可以重新实现它。如果你这样做,试着使用苹果的变量命名。这样,每个人都会立即知道如何使用您的类。

尽管您可以将UIPageControl子类化,但我认为它不会添加任何内容。我建议您将UIControll子类化,并将API基于UIPAgeControl的API。我认为UI应该是相同的


您可以添加属性来控制文本,但可能不需要添加属性。

只需使用UILabel并在滚动委托中设置文本w/a格式字符串,该字符串表示“第N页,共K页”或类似内容。不可见的左/右按钮可以添加简单的透明
ui按钮
s


如果您真的想自己编写UIControl子类,则由您决定……

此外,我应该为任何考虑将UIPageControl子类化的人添加(正如我在这样做之后(不幸地)来到这里的那样),出于某些愚蠢的原因,该类不会在drawRect:或任何其他公共方法中绘制。我怀疑在页面更新过程中的某个时刻,它会设置一个绘图上下文并用其点填充,但我在此警告您:

  • 不能重写UIPageControl中的drawRect:以便绘制自定义点而不是标准点。即使您让draw rect绘制点,UIPageControl也将始终(耶!)绘制其小白点
这使得该类本质上不可子类化。当然可以,但是为什么

更重要的是,为什么苹果会用如此荒谬的反常行为来制造一个班级?!drawRect:这是有原因的!这让我想起了在使用imagebrowser之前的日子里使用NSMatrix…

编辑:

这个答案获得了好几票赞成票。谢谢大家,但是对于所有来到这里的人,请意识到UIPageControl非常容易自己实现!更重要的是,看看下面我的回答和示例代码,了解一些想法,但您确实不应该在生产代码中使用它。如果需要修改UIPageControl的外观和感觉,请滚动您自己的UIControl子类


原始答复:

UIPageControl没有做任何花哨的事情。对于大多数视图,视图本身要么在drawRect中进行所有绘制,要么添加子视图,并自行绘制。通过重写drawRect:而不调用[super drawRect:],可以确保如果实现是基于drawRect:-的,则可以调用自己的绘图代码。对于UIPageControl,它依赖于已添加到UIPageControl的子视图。你不需要知道是哪一种,也不应该在意。这是苹果的实施,可能会发生变化

但是,因为您知道它必须是基于drawRect:或子视图的,所以只需删除UIPageControl的子视图就可以了,然后您的drawRect:重写将如您所期望的那样工作(大多数情况下,您必须做一些额外的工作以确保在正确的时间重新绘制)

下面的示例演示如何创建自己的自定义UIPageControl。精明的读者会注意到,一旦你不厌其烦地以这种方式进行操作,你也可以创建自己的页面控件作为UIControl的子类,并简单地实现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
//
//RedGreyPageControl.m
//
@接口RedGreyPageControl:UIPageControl{
NSArray*原始子视图;
}
@结束
@实现RedGreyPageControl
//这假定您是从nib创建控件。取决于你的
//用法您可以在initWithFrame中执行此步骤:
-(无效)从NIB中唤醒{
//保留原始子视图,以防apple实施
//依赖于由视图的
//在它的超视界中的存在。
originalSubviews=[[NSArray alloc]initWithArray:self.subviews];
对于(UIView*self.subview中的视图)[view removeFromSuperview];
//确保在旋转设备时重新绘制视图,而不是缩放视图
self.contentMode=UIViewContentModeRedraw;
}
-(无效)解除锁定{
[原始子视图发布];
[super dealoc];
}
-(void)drawRect:(CGRect)direct{
UIImage*灰色,*图像,*红色;
int i;
CGRect rect;
常数CGFloat kSpacing=10.0;
direct=自界;
if(自不透明){
[自我背景色设置];
UIRectFill(direct);
}
if(self.hidesForSinglePage&&self.numberOfPages==1)返回;
红色=[UIImage imageNamed:@“circle_graphic_red.png”];
灰色=[UIImage imageNamed:@“circle_graphic_grey.png”];
rect.size.height=red.size.height;
rect.size.width=self.numberOfPages*red.size.width+(self.numberOfPages-1)*k间隔;
rect.origin.x=地板((direct.size.width-rect.size.width)/2.0);
矩形原点y=