文档未记录的API之UICalloutView

本文深入探讨了iOS应用中一个未在文档中记录的UI元素——UICalloutView。它是一种用于指向屏幕内容的UIControl实例,能够显示临时消息并具有扩展按钮。文章解释了如何创建、配置和使用此类元素,包括添加目标、调整位置、显示和隐藏操作等关键功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你在iPhone上使用过Google Map,你可能在实战中见过UICalloutView实例。虽然它们的名称如此,但是它们是一种UIControl实例。它们是文档中未记录的,但在UIKit框架中可用。

标注视图指向屏幕上的某些内容。它们在使用附加的扩展按钮移动到另一个消息之前,可以显示一个临时消息。下图显示了带有几个标注视图的屏幕,其中有些标注视图显示它们的源(临时)消息,其他标注视图显示最终消息。

由于类是文档中未记录的,因此需要定义头文件。下面是UICalloutView类定义。它还不完整,不过它提供了足够的功能使你能够在程序中有效地使用这个类。

@interface UICalloutView : UIControl - (UICalloutView *)initWithFrame:(struct CGRect)aFrame; - (void)fadeOutWithDuration:(float)duration; - (void)setTemporaryTitle:(NSString *)fp8; - (NSString *)temporaryTitle; - (void)setTitle:(NSString *)fp8; - (NSString *)title; - (void)setSubtitle:(NSString *)fp8; - (NSString *)subtitle; - (void)addTarget:(id)target action:(SEL)selector; - (void)removeTarget:(id)target; - (void)setDelegate:(id)delegate; - (id)delegate; - (void)setAnchorPoint:(struct CGPoint)aPoint boundaryRect:(struct CGRect)aRect animate:(BOOL)yorn; @end

要创建一个标注,需要传递一个定位点--即标注指向的坐标--和一个广义(generalized)边界矩形。据我所知,这些限制完全被忽略了。

标注使用两个标题:一个临时标题和一个普通标题。当添加一个临时标题时,此标题显示大约一秒钟的时间,然后会变为普通标题,即带有扩展按钮的标题。

通过调用addTarget: action:方法,为扩展按钮设置目标。当扩展按钮被点击时,就调用传递的选择程序。这里不为标注上的用户点击计数,iPhone忽略了它们。因此,如 果希望在出现一个新标注时除去现有标注,应该由你来消除它。可以快速地使用fadeOutWithDuration:方法并指定渐没时间。

下面是使用的办法

- (void) hideDisclosure: (UIPushButton *) calloutButton { UICalloutView *callout = (UICalloutView *)[calloutButton superview]; [callout fadeOutWithDuration:1.0f]; } - (UICalloutView *) buildDisclosure { UICalloutView *callout = [[[UICalloutView alloc] initWithFrame:CGRectZero] autorelease]; callout.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; callout.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // You can easily customize the information and the target [callout setTemporaryTitle:@"You Tapped Here!"]; [callout setTitle:@"More Info..."]; [callout addTarget:self action:@selector(hideDisclosure:)]; return callout; } - (void) showDisclosureAt:(CGPoint) aPoint { UICalloutView *callout = [self buildDisclosure]; [self addSubview:callout]; [callout setAnchorPoint:aPoint boundaryRect:CGRectMake(0.0f, 0.0f, 320.0f, 100.0f) animate:YES]; } - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { [self showDisclosureAt:[[touches anyObject] locationInView:self]]; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值