ios新手必看之手势

来源:互联网 发布:淘宝手机怎么删除差评 编辑:程序博客网 时间:2024/05/21 13:59

//    自定义视图

//    视图可以通过从写Touch方法实现响应点击

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@property (nonatomic,assign)CGFloat myPoint;

@end


 self.view.backgroundColor = [UIColorwhiteColor];

   UIView *view = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

//    设置Tag标记

    view.tag = 100;

    view.backgroundColor = [UIColoryellowColor];

    [self.viewaddSubview:View];

    [view release];//MRC不要忘了释放

    

    

    

}

//释放

- (void)dealloc {

    

    [super dealloc];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"触摸开始");

//    实现清扫屏幕(实例)

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch1 locationInView:self.view];

    _myPoint = point.x;

//    label.userInteractionEnabled = YES;//打开交互

//    ui里面有辆特殊的view—UILabel(标签), UIImageVIew(图片),这两视图默认没有交互,就是默认完全不响应用户事件的。

/*

     响应者链的顺序,从底层向上层寻找,找到发生时间的对象,然后依次向底层处理。响应者链可以阻断。(userInteractionEnabled),它会逐层返回至Application,系统确认为无效事件并销毁完成整个过程。

*/

    

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"触摸取消");

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"触摸结束");

////    获取视图通过Tag值

//    UIView *view = (UIView *)[self.view viewWithTag:100];

////    定义touch从集合取出

//    UITouch *touch = [touches anyObject];

////    通过touch获取相对于父视图的位置

//    CGPoint point = [touch locationInView:self.view];

////    设置中心点

//    view.center = point;

    

//    清扫

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    if (point.x -_myPoint >= 200 ) {

    UIView *view = (UIView *)[self.viewviewWithTag:100];

      [view removeFromSuperview];//删除view实现清扫

    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"正在触摸");

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"摇一摇开始");

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"摇一摇结束");

//   实现摇一摇换色

    UIView *view = (UIView *)[self.viewviewWithTag:100];

    view.backgroundColor = [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0blue:arc4random()%256/255.0alpha:0.8];


}

@end

0 0
原创粉丝点击