ios5挥动实例

ios5摇动实例

以下用ios5实现摇动事件,关键代码如下(附件中有工程代码 ):

 

//推送摇动消息事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motionBegan=============");
    if (motion == UIEventSubtypeMotionShake) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
    }
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motionEnded=============");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motionCancelled==============");
}

 

//绑定摇动后执行方法(该语句非常重要)

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addShakeAnimations) name:@"shake" object:nil];

 

以下为ios学习小结:

1、ios4工程在ios5上运行会出现ARC问题,在Compile Sources 下的Compiler Flags属性中添加-fno-objc-arc,可解决该问题。

 

2、UIImageView 上添加点击事件

      UIImageView *imageView =[[UIImageView alloc ]initWithFrame :CGRectMake (100 , 100 , 200 , 200 )];

    imageView. image =[ UIImage imageNamed : @"test.png"];

     //事件可用设置

    imageView. userInteractionEnabled = YES ;

    UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (clickImage)];

    [imageView addGestureRecognizer :tap];

 

3、ios5 UIView 设圆角需添加#import <QuartzCore/QuartzCore.h>

centerView.layer.cornerRadius = 6.0f;