Skip to content

Commit 0213228

Browse files
take the accessibilityActivation point into consideration when tapping elements (#1305)
1 parent 8f094a6 commit 0213228

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

Sources/KIF/Classes/KIFUITestActor.m

+8
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,14 @@ - (CGRect) elementFrameForElement:(UIAccessibilityElement *)element andView:(UIV
16731673

16741674
- (CGPoint) tappablePointInElement:(UIAccessibilityElement *)element andView:(UIView *)view
16751675
{
1676+
// AccessibilityActivationPoint indicates where on the element assistive technologies should issue a tap event to activate the element.
1677+
// In the case where the property has not been explicitly set. The default value is the midpoint of the accessibility frame.
1678+
UIView *hitView = [view.window hitTest:element.accessibilityActivationPoint withEvent:nil];
1679+
if ([view isTappableWithHitTestResultView:hitView]) {
1680+
return [view.window convertPoint:element.accessibilityActivationPoint toView:view];
1681+
}
1682+
1683+
// If the element's AccessibilityActivationPoint is not tappable, attempt to find a suitable tappable point within the element's frame.
16761684
CGRect elementFrame = [self elementFrameForElement:element andView:view];
16771685
CGPoint tappablePoint = [view tappablePointInRect:elementFrame];
16781686

TestHost/AccessibilityViewController.m

+39-20
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ @interface AccessibilityViewController_AccessibilityView : UIView
1111
@property (nonatomic, assign) BOOL activationReturnValue;
1212
@property (nonatomic, assign) int activationCount;
1313

14-
@property (nonatomic, strong) UILabel *label;
14+
@property (nonatomic, strong) UILabel *topLabel;
15+
@property (nonatomic, strong) UILabel *swtichLabel;
1516
@property (nonatomic, strong) UISwitch *activationSwitch;
1617

17-
1818
@end
1919

2020

@@ -24,16 +24,16 @@ - (instancetype)initWithCoder:(NSCoder *)coder {
2424
self = [super initWithCoder:coder];
2525
self.isAccessibilityElement = YES;
2626
self.accessibilityLabel = @"AccessibilityView";
27-
28-
27+
2928
self.activationReturnValue = YES;
30-
31-
self.label = [[UILabel alloc] initWithFrame: CGRectZero];
32-
[self addSubview:self.label];
33-
34-
self.backgroundColor = [UIColor systemTealColor];
35-
self.label.text = @"Returns YES";
3629

30+
self.topLabel = [[UILabel alloc] initWithFrame: CGRectZero];
31+
self.topLabel.text = @"Awaiting activation or tap";
32+
[self addSubview:self.topLabel];
33+
34+
self.swtichLabel = [[UILabel alloc] initWithFrame: CGRectZero];
35+
self.swtichLabel.text = @"Returns YES";
36+
[self addSubview:self.swtichLabel];
3737

3838
self.activationSwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
3939
[self addSubview: self.activationSwitch];
@@ -50,35 +50,54 @@ - (void)toggleReturnValue {
5050

5151
if (self.activationReturnValue == YES) {
5252
self.backgroundColor = [UIColor systemTealColor];
53-
self.label.text = @"Returns YES";
53+
self.swtichLabel.text = @"Returns YES";
5454
} else {
5555
self.backgroundColor = [UIColor systemTealColor];
56-
self.label.text = @"Returns NO";
56+
self.swtichLabel.text = @"Returns NO";
5757
}
5858
[self setNeedsLayout];
5959
}
6060

6161
-(void)layoutSubviews {
6262
[super layoutSubviews];
63-
[self.label sizeToFit];
64-
self.label.frame = CGRectMake((self.frame.size.width - self.label.frame.size.width) / 2,
65-
(self.frame.size.height - self.label.frame.size.height) / 2,
66-
self.label.frame.size.width,
67-
self.label.frame.size.height);
63+
[self.topLabel sizeToFit];
64+
self.topLabel.frame = CGRectMake(20,
65+
20,
66+
self.topLabel.frame.size.width,
67+
self.topLabel.frame.size.height);
68+
69+
[self.swtichLabel sizeToFit];
70+
self.swtichLabel.frame = CGRectMake(20,
71+
CGRectGetMaxY(self.topLabel.frame) + 40,
72+
self.swtichLabel.frame.size.width,
73+
self.swtichLabel.frame.size.height);
6874

6975
[self.activationSwitch sizeToFit];
70-
self.activationSwitch.frame = CGRectMake((self.frame.size.width - self.activationSwitch.frame.size.width) / 2,
71-
CGRectGetMaxY(self.label.frame) + 10 ,
76+
self.activationSwitch.frame = CGRectMake(20,
77+
CGRectGetMaxY(self.swtichLabel.frame) + 10 ,
7278
self.activationSwitch.frame.size.width,
7379
self.activationSwitch.frame.size.width);
80+
81+
}
82+
83+
- (NSString *)accessibilityValue {
84+
return self.topLabel.text;
7485
}
7586

7687
- (BOOL)accessibilityActivate {
7788
self.activationCount += 1;
78-
self.accessibilityValue = [NSString stringWithFormat:@"Activated: %i", self.activationCount];
89+
self.topLabel.text = [NSString stringWithFormat:@"Activated: %i", self.activationCount];
90+
[self setNeedsLayout];
7991
return self.activationReturnValue;
8092
}
8193

94+
95+
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
96+
CGPoint location = [[touches anyObject] locationInView: self];
97+
self.topLabel.text = [NSString stringWithFormat:@"Tapped - x:%.04f, y:%.04f", location.x, location.y];
98+
[self setNeedsLayout];
99+
}
100+
82101
@end
83102

84103
@interface AccessibilityViewController : UIViewController

TestHost/Base.lproj/MainStoryboard.storyboard

+7-7
Original file line numberDiff line numberDiff line change
@@ -2170,26 +2170,26 @@ Line Break
21702170
<viewControllerLayoutGuide type="top" id="shx-5S-eMw"/>
21712171
<viewControllerLayoutGuide type="bottom" id="u8z-PF-PIc"/>
21722172
</layoutGuides>
2173-
<view key="view" contentMode="scaleToFill" id="HtV-3z-P6p" customClass="AccessibilityViewController_AccessibilityView">
2173+
<view key="view" contentMode="scaleToFill" id="HtV-3z-P6p">
21742174
<rect key="frame" x="0.0" y="0.0" width="414" height="804"/>
2175-
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2175+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES" flexibleMaxY="YES"/>
21762176
<subviews>
2177-
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1Qj-vC-6qF">
2178-
<rect key="frame" x="72" y="367" width="240" height="128"/>
2179-
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2177+
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1Qj-vC-6qF" customClass="AccessibilityViewController_AccessibilityView">
2178+
<rect key="frame" x="20" y="76" width="374" height="657"/>
2179+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
21802180
<color key="backgroundColor" systemColor="systemPurpleColor"/>
21812181
</view>
21822182
</subviews>
21832183
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
21842184
</view>
21852185
<navigationItem key="navigationItem" id="4hs-zD-BI5"/>
21862186
<connections>
2187-
<outlet property="accessibilityView" destination="HtV-3z-P6p" id="fRH-1a-uNU"/>
2187+
<outlet property="accessibilityView" destination="1Qj-vC-6qF" id="YDb-m4-vKE"/>
21882188
</connections>
21892189
</viewController>
21902190
<placeholder placeholderIdentifier="IBFirstResponder" id="N4E-Ay-Je9" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
21912191
</objects>
2192-
<point key="canvasLocation" x="1967" y="1449"/>
2192+
<point key="canvasLocation" x="1966.6666666666667" y="1448.4375"/>
21932193
</scene>
21942194
<!--Animation View Controller-->
21952195
<scene sceneID="odg-A0-FBB">

Tests/AccessibilityActivationTests_ViewTestActor.m

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
#import <KIF/KIF.h>
9+
#import <UIKit/UIAccessibility.h>
910

1011
@interface AccessibilityActivateTests_ViewTestActor : KIFTestCase
1112
@end
@@ -33,4 +34,17 @@ - (void)testAccessibilityActivate
3334
[[viewTester usingValue:@"Activated: 2"] waitForView];
3435
}
3536

37+
- (void)testAccessibilityActiationPoint
38+
{
39+
UIView *view = [[viewTester usingValue: @"Awaiting activation or tap"] waitForView];
40+
[view setAccessibilityActivationPoint: [view.window convertPoint:CGPointMake(25.0, 50.0) fromView:view]];
41+
42+
[[viewTester usingLabel: @"AccessibilityView"] tap];
43+
[[viewTester usingValue:@"Tapped - x:25.0000, y:50.0000"] waitForView];
44+
45+
[view setAccessibilityActivationPoint: [view.window convertPoint:CGPointMake(50.0, 25.0) fromView:view]];
46+
[[viewTester usingLabel: @"AccessibilityView"] tap];
47+
[[viewTester usingValue:@"Tapped - x:50.0000, y:25.0000"] waitForView];
48+
}
49+
3650
@end

0 commit comments

Comments
 (0)