// for (NSString * str in [UIFont familyNames]) {
// NSLog(@"%@",str);
// }
// NSLog(@"%@",[UIFont familyNames]);
tap 点击
longpress 长按
pan 拖拽
rotation 旋转
pinch 捏合
swipe 清扫
screenEedgePan 屏幕边缘拖拽
UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 280, 420)];
[self.view addSubview:image];
[image release];
[image setImage:[UIImage imageNamed:@"3.jpg"]];
//打开视图的交互事件
[image setUserInteractionEnabled:YES];
//点击手势
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[image addGestureRecognizer:tap];
[tap release];
//设置触发方法所需要的点击次数
tap.numberOfTapsRequired = 3;
//设置触发点数目
tap.numberOfTouchesRequired = 2;
//长按
UILongPressGestureRecognizer * longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[image addGestureRecognizer:longpress];
[longpress release];
//长按有效时间
longpress.minimumPressDuration = 3;
//允许移动范围
longpress.allowableMovement = 200;
//轻扫
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[image addGestureRecognizer:swipe];
[swipe release];
//方向
swipe.direction = UISwipeGestureRecognizerDirectionUp |UISwipeGestureRecognizerDirectionDown;
//旋转
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[image addGestureRecognizer:rotation];
[rotation release];
//捏合
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[image addGestureRecognizer:pinch];
[pinch release];
//拖拽
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[image addGestureRecognizer:pan];
[pan release];
// for (NSString * str in [UIFont familyNames]) {
// NSLog(@"%@",str);
// }
// NSLog(@"%@",[UIFont familyNames]);
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
if (self.view.backgroundColor == [UIColor cyanColor]) {
[self.view setBackgroundColor:[UIColor blackColor]];
}else{
[self.view setBackgroundColor:[UIColor cyanColor]];}
}
-(void)longPressAction:(UILongPressGestureRecognizer *)longpress
{
[self.view setBackgroundColor:[UIColor blueColor]];
// NSLog(@"%s",__FUNCTION__);
NSLog(@"按住");
if (longpress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}
if (longpress.state == UIGestureRecognizerStateEnded) {
NSLog(@"拜拜~");
}
}
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
if (self.view.backgroundColor == [UIColor blueColor]) {
[self.view setBackgroundColor:[UIColor greenColor]];
}else{
[self.view setBackgroundColor:[UIColor blueColor]];}
}
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform,rotation.rotation);
rotation.rotation = 0;
}
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
-(void)panAction:(UIPanGestureRecognizer *)pan
{
CGPoint p = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
[pan setTranslation:CGPointZero inView:pan.view];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([_target respondsToSelector:_action]) {
//没有参数的方法设置
// [_target performSelector:_action];
//有参数的方法设置
[_target performSelector:_action withObject:self];
}
}
-(void)addTarget:(id)target action:(SEL)action
{
_target = target;
_action = action;
}