Objective-C,重复相同操作但旋转90度

时间:2013-05-08 22:35:03

标签: objective-c uikit

我有一些代码,这是一个很小的摘录:

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转换为方法,但我需要为所有依赖项重复此操作。其他想法?

1 个答案:

答案 0 :(得分:0)

使用标准的OO实践并使方法足够通用,您可以轻松地实现这一目标。这是继承的相当不错的使用

// 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];
}