2013-05-08 35 views
1

我有一些代碼,這是一個很小的摘錄:的Objective-C,重複相同的操作,但旋轉90度

surfaceDimension = self.window.frame.size.height; 
sliderDistancePerPoint = 1.0f/surfaceDimension/FINGER_RATIO; 
addThisToSlider = sliderDistancePerPoint * diff * 1.6f; 

在子類中,我覆蓋此方法,並重復相同的代碼,但旋轉:

surfaceDimension = self.window.frame.size.width; 
sliderDistancePerPoint = 1.0f/surfaceDimension/FINGER_RATIO; 
addThisToSlider = sliderDistancePerPoint * diff * 1.6f; 

什麼是(各種)方式,我可以虛擬化此代碼,以便它可以在任一方向工作?我當然可以使surfaceDimension成爲一種方法,但我需要對所有依賴關係重複此操作。其他想法?

回答

0

使用標準的面向對象實踐並使這些方法足夠普遍,可以很容易地實現這種移動。這是一個相當不錯的使用繼承

// override in subclass 
- (BOOL) useXOrientation { 
    return NO; 
} 

- (CGPoint) setOriginFrom:(CGPoint)sourcePoint to:(CGPoint)targetPoint { 
    if (self.useXOrientation) 
     targetPoint.x = sourcePoint.x; 
    else 
     targetPoint.y = sourcePoint.y; 
    return targetPoint; 
} 



- (float) sizeOfDimension:(CGSize)size { 
    if (self.useXOrientation) 
     return size.width; 
    else 
     return size.height; 
} 

// example method 
- (void) resetFirstTouch:(CGPoint)point { 
    self.firstTouch = [self setOriginFrom:point to:self.firstTouch]; 
    self.firstTouchInParent = [self setOriginFrom:[self convertPoint:self.firstTouch toView:self.superview] to:self.firstTouchInParent]; 
}