我在我的項目中使用UISpec進行自動測試。一切都很好,直到Apple發佈帶有iOS 6 SDK的xCode 4.5。現在UISpec項目無法編譯,因爲UITouch類的錯誤列表。以下是代碼:以編程方式在xCode 4.5中模擬UITouch(UISpec)
//
// TouchSynthesis.m
// SelfTesting
//
// Created by Matt Gallagher on 23/11/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file, free of charge, in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
@implementation UITouch (Synthesize)
//
// initInView:phase:
//
// Creats a UITouch, centered on the specified view, in the view's window.
// Sets the phase as specified.
//
- (id)initInView:(UIView *)view
{
self = [super init];
if (self != nil)
{
CGRect frameInWindow;
if ([view isKindOfClass:[UIWindow class]])
{
frameInWindow = view.frame;
}
else
{
frameInWindow =
[view.window convertRect:view.frame fromView:view.superview];
}
_tapCount = 1;
_locationInWindow =
CGPointMake(
frameInWindow.origin.x + 0.5 * frameInWindow.size.width,
frameInWindow.origin.y + 0.5 * frameInWindow.size.height);
_previousLocationInWindow = _locationInWindow;
UIView *target = [view.window hitTest:_locationInWindow withEvent:nil];
_window = [view.window retain];
_view = [target retain];
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
return self;
}
//
// setPhase:
//
// Setter to allow access to the _phase member.
//
- (void)setPhase:(UITouchPhase)phase
{
_phase = phase;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
//
// setPhase:
//
// Setter to allow access to the _locationInWindow member.
//
- (void)setLocationInWindow:(CGPoint)location
{
_previousLocationInWindow = _locationInWindow;
_locationInWindow = location;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
@end
編譯器在行上提供了諸如「_tapCount = 1;」的錯誤錯誤是「Uknown type name」_tapCount「;你的意思是......?」
我做了什麼:
- 獲取UITouch類實例變量的列表,使用運行時間功能「class_copyIvarList」,並檢查是否這些變數仍然存在。他們是這樣。
試圖在運行時改變所需要的值,比如
UINT U相= UITouchPhaseBegin; object_setInstanceVariable(self,「_phase」,& uPhase); object_setInstanceVariable(self,「_phase」,& uPhase);
或
Ivar var = class_getInstanceVariable([self class], "_phase");
object_setIvar(self, var, [NSNumber numberWithInt:UITouchPhaseBegin]);
在這兩種情況下無效的值被寫入 「_phase」 構件。我通過將控件的UITouch實例的描述打印出來進行檢查。它被寫成「階段:Uknown」。
然後我決定嘗試鍵 - 值編碼,並實現了它想:
[self setValue:[NSNumber numberWithInt:1] forKey:@"tapCount"];
[self setValue:[NSNumber numberWithInt:UITouchPhaseBegin] forKey:@"phase"];
現在它給了一些成果。編譯錯誤被消除,UITouch實例的成員值被改變但仍然沒有任何反應。我在適當的UI對象(在我的例子中是UIButton)的「touchesBegan:...」和「touchesEnded:...」方法中設置了斷點並對其進行了調試。這些方法被調用,但沒有任何反應 - 按鈕處理程序不被調用。
也由於KVC我不得不評論UITouch類別的方法「setPhase」和「setLocationInWindow」,否則會發生無盡的遞歸。財產的二傳手自稱。
現在我沒有想法。這個類別是「由Matt Gallagher於23/11/08創建的」,這意味着它是UISpec的第三方代碼。所以我希望它在除了UISpec之外的其他地方使用,並且有人知道工作。
謝謝。
P.S.我知道這是一個私人API。它不包含在項目的發佈配置中。我只需要它進行自動測試
完美的作品。非常感謝! – alfared