在objc中使用struct

来源:互联网 发布:表情图片制作软件下载 编辑:程序博客网 时间:2024/04/28 16:01

在java中,向方法中传递复杂参数,一般使用对象来传递。因为这样便于扩展。不需要改变方法的声明部分。

在objc中,开始是发送多个参数。这样很麻烦。那么可以用到c里面的方式来做,通过struct传递参数。把相关的参数写在一个struct里面。

声明struct需要在头文件中:

#define MIN_VELOCITY 10 
#define LOOP_COUNT 100

@interface AnimationView : UIView <UIGestureRecognizerDelegate>{ 
    CALayer *startLayer; 
    CALayer *startLayer2; 
}

struct PanLocationData{ 
    CGPoint currentLocation; 
    CGPoint currentVelocity; 
    CGPoint panLocation; 
};

在方法的参数中使用struct:

- (CGPoint) getNextPanLocation:(struct PanLocationData) data;

创建struct的代码:

struct PanLocationData panData; 
panData.panLocation=locationInView; 
panData.currentVelocity=velocityInView; 
panData.currentLocation=startLayer.position;

[CATransaction begin]; 
[CATransaction setValue:[NSNumber numberWithBool:YES] 
                 forKey:kCATransactionDisableActions];

startLayer.position=[self getNextPanLocation:panData]; 
[CATransaction commit];

if (gestureRecognizer.state==UIGestureRecognizerStateEnded) { 
    [self doPostPanAction:panData]; 
}