读书人

UI各种手势(轻拍长按旋转假造

发布时间: 2012-12-20 09:53:21 作者: rapoo

UI各种手势(轻拍,长按,旋转,捏合,清扫,拖拽)

-(void)loadView

{

self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

self.view.backgroundColor = [UIColor redColor];

imageV = [[UIImageView alloc]initWithFrame:CGRectMake(70, 150, 200, 200)];

imageV.backgroundColor = [UIColor blueColor];

[self.view addSubview:imageV];

[imageV release];

NSArray *arr = [NSArray arrayWithObjects:@"轻拍",@"长按",@"旋转",@"捏合",@"轻扫",@"拖拽", nil];

UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:arr];

[seg addTarget:self action:@selector(tap:) forControlEvents:UIControlEventValueChanged];

seg.frame = CGRectMake(0, 400, 320, 40);

imageV.userInteractionEnabled = YES;

[self.view addSubview:seg];

}

-(void)tap:(UISegmentedControl *)tap

{

for(id gesture in imageV.gestureRecognizers)

{

[imageV removeGestureRecognizer:gesture];

}

switch (tap.selectedSegmentIndex)

{

case 0:

{

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

[imageV addGestureRecognizer:tap];

imageV.userInteractionEnabled = YES;

[tap release];

break;

}

case 1:

{

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

[imageV addGestureRecognizer:longPress];

[longPress release];

break;

}

case 2:

{

UIRotationGestureRecognizer *rotationPress = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationPress:)];

[imageV addGestureRecognizer:rotationPress];

[rotationPress release];

break;

}

case 3:

{

UIPinchGestureRecognizer *pinchPress = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchPress:)];

[imageV addGestureRecognizer:pinchPress];

[pinchPress release];

break;

}

case 4:

{

UISwipeGestureRecognizer *swipePress = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipePress:)];

[imageV addGestureRecognizer:swipePress];

[swipePress release];


UISwipeGestureRecognizer *swipePress1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipePress:)];

[imageV addGestureRecognizer:swipePress1];

swipePress.direction = UISwipeGestureRecognizerDirectionLeft ;

[swipePress1 release];

break;

}

case 5:

{

UIPanGestureRecognizer *panPress = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPress:)];

[imageV addGestureRecognizer:panPress];

[panPress release];

break;

}

default:

break;

}

}

-(void)dotap:(UITapGestureRecognizer *)dotap//轻按随机换图

{

NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg",arc4random()%8+1];

imageV.image = [UIImage imageNamed:imageName];

}

-(void)longPress:(UILongPressGestureRecognizer *)longPress//长按按顺序换图

{

if(longPress.state == UIGestureRecognizerStateEnded)

{


NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg",i%8+1] ;

imageV.image = [UIImage imageNamed:imageName];

i = i+1;

}

}

-(void)rotationPress:(UIRotationGestureRecognizer *)rotationPress//旋转

{

[imageV setTransform:CGAffineTransformMakeRotation(rotationPress.rotation)];

}


-(void)pinchPress:(UIPinchGestureRecognizer *)pinchPress//捏合

{

[imageV setTransform:CGAffineTransformMakeScale(pinchPress.scale, pinchPress.scale)];

}


-(void)swipePress:(UISwipeGestureRecognizer *)swipePress//轻扫

{

[UIView beginAnimations:nil context:nil];

[UIView setAnimationTransition:(swipePress.direction == UISwipeGestureRecognizerDirectionLeft )?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight forView:imageV cache:YES];

[UIView commitAnimations];

}

-(void)panPress:(UIPanGestureRecognizer *)panPress //拖拽

{

CGPoint point =[panPress translationInView:imageV];

imageV.transform = CGAffineTransformMakeTranslation(point.x, point.y);

}


读书人网 >移动开发

热点推荐