我正在尝试使用VoiceOver访问一个简单的应用程序。
该应用加载具有不透明视图的视图控制器,并跟踪该视图上一个手指的位置。但是我无法理解为什么我必须重新聚焦(通过点击)我的视图,即使在调用'(void)accessibilityElementDidBecomeFocused'方法之后也是如此。我很困惑,因为我的视图'(void)accessibilityElementDidLoseFocus'也从未被调用过。
我相信我正确地遵循了SO和Apple文档中的View Controller遏制说明:
2 - How does View Controller Containment work in iOS 5?
显示新视图控制器的代码如下:
CGRect frame = self.view.bounds;
customViewController = [[FocusIssueCustomViewController alloc] init];
[customViewController loadWithFrame:frame forViewController:self withAlpha:0.5];
// setting properties of the custom view:
[customViewController.view setAccessibilityLabel:@"Touch area"];
[customViewController.view setMultipleTouchEnabled:YES];
[customViewController.view setIsAccessibilityElement:YES];
[customViewController.view setAccessibilityTraits:UIAccessibilityTraitAllowsDirectInteraction];
// inform user the screen has changed:
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, customViewController.view);
和loadWithFrame方法如下:
- (void) loadWithFrame: (CGRect) frame forViewController: (UIViewController*) viewController withAlpha: (float) alphaValue
{
// keep a reference to the view controller that requested the Custom View
[self setOriginatingViewController:viewController];
// load the custom view
[self setCustomView:[[FocusIssueCustomView alloc] initWithFrame:frame]];
[self customView].alpha = alphaValue;
// make this the ViewController for this view
self.view = [self customView];
// add the view controller following guidelines from the
// viewController containment in the UIViewController class reference from the Apple docs
[self.originatingViewController addChildViewController:self];
[self.originatingViewController.view addSubview:self.view];
[self didMoveToParentViewController:self.originatingViewController];
}
此自定义视图会跟踪一次触摸:实现文件如下所示。
#import "FocusIssueCustomView.h"
#define CIRCLE_DIAMETER 110
@interface FocusIssueCustomView ()
{
// coordinate of this one touch
float xCood, yCood;
}
@end
@implementation FocusIssueCustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// make the custom view opaque
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat black[] = {0, 0, 0, 1.0};
CGContextSetFillColor(ctx, black);
CGContextAddRect(ctx, rect);
CGContextFillPath(ctx);
[self setAlpha:0.5];
// clear current context
CGContextRef CurrentContext = UIGraphicsGetCurrentContext();
CGContextClearRect(CurrentContext, rect);
// draw a blue circle, no fill for the touch
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetRGBStrokeColor(context, 0, 0, 255, 1);
CGRect rectangle = CGRectMake(xCood - CIRCLE_DIAMETER/2, yCood - CIRCLE_DIAMETER/2, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
- (void) accessibilityElementDidBecomeFocused
{
NSLog(@"Inside the custom view's \"accessibilityElementDidBecomeFocused\" method");
}
- (void) accessibilityElementDidLoseFocus
{
NSLog(@"Inside the custom view's \"accessibilityElementDidLoseFocus\" method");
}
#pragma mark - touch method
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// if 1 touch on screen, track this touch
if ([[event allTouches] count] == 1)
{
CGPoint point = [[touches anyObject] locationInView:self];
xCood = point.x;
yCood = point.y;
NSLog(@"Touch at (%f, %f)", xCood, yCood);
}
// redraw
[self setNeedsDisplay];
}
@end
我真的需要一个视图控制器来控制这个透明的CustomView(因此视图控制器包含方法)。这就是为什么我不是简单地采用'addSubview'方法。非常感谢任何帮助..
谢谢 -
VP