1

我正在尝试使用 VoiceOver 制作一个简单的应用程序。

该应用程序加载一个带有不透明视图的视图控制器,并在此视图上跟踪一根手指的位置。但是,我无法理解为什么我必须重新关注(通过点击)我的视图,即使在调用了它的“(void)accessibilityElementDidBecomeFocused”方法之后也是如此。我很困惑,因为我的视图的“(void)accessibilityElementDidLoseFocus”也从未被调用过。

我相信我正确地遵循了来自 SO 和 Apple 文档的 View Controller 遏制说明:

1 - http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW81

2 -视图控制器包含如何在 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”方法的原因。任何帮助是极大的赞赏..

谢谢 -

副总裁

4

0 回答 0