【视图与UITouch对应的方法】
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取点击事件
UITouch *t = [touches anyObject];
//如果点击的是图片就把他移到视图的最上层
if ([t.view isKindOfClass:[UIImageView class]]) {
[self.view bringSubviewToFront:t.view];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
//如果点击的是图片就让他移动
if ([t.view isKindOfClass:[UIImageView class]]) {
CGPoint p = [t locationInView:self.view];
t.view.center = p;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
//点到第二次的时候,如果点击的是图片就让图片变长
if ([t.view isKindOfClass:[UIImageView class]]) {
if (t.tapCount == 2) {
CGRect rect = t.view.frame;
rect.size.height += 10;
t.view.frame = rect;
}
}
}
【UIGestureRecognizer】手势识别器
6种手势
UITapGestureRecognizer Tap(点击)
UIPanGestureRecognizer Pan (拖移,移动)
UIPinchGestureRecognizer Pinch(缩放)
UIRotationGestureRecognizer Rotation(旋转)
UILongPressGestureRecognizer LongPress(长按)
UISwipeGestureRecognizer Swipe(轻扫)
- (void)tapGR:(UITapGestureRecognizer *)tapGR
{
NSLog(@"点一下");
[self.view bringSubviewToFront:tapGR.view];
}
- (void)panGR:(UIPanGestureRecognizer *)panGR
{
NSLog(@"拖动");
CGPoint translation = [panGR translationInView:self.view];
panGR.view.center = CGPointMake(panGR.view.center.x + translation.x,panGR.view.center.y + translation.y);
[panGR setTranslation:CGPointZero inView:self.view];
}
- (void)pinchGR:(UIPinchGestureRecognizer *)pinchGr
{
NSLog(@"缩放");
pinchGr.view.transform = CGAffineTransformScale(pinchGr.view.transform, pinchGr.scale, pinchGr.scale);
pinchGr.scale = 1;
}
- (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR
{
NSLog(@"旋转");
rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);
rotationGR.rotation = 0;
}
- (void)lpGR:(UILongPressGestureRecognizer *)lpGR
{
NSLog(@"长按");
if (lpGR.state == UIGestureRecognizerStateEnded) {
CABasicAnimation *shake=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
//transform.scale代表缩放(倍数)
//transform.rotation.x transform.rotation.y transform.rotation.z 代表旋转(角度)
//动画的初始值和结束值
shake.fromValue=[NSNumber numberWithFloat:0.5];
shake.toValue=[NSNumber numberWithFloat:1.5];
//单次时间
shake.duration=0.2;
//重复次数
shake.repeatCount=15;
//动画返回
shake.autoreverses=YES;
//添加动画
[lpGR.view.layer addAnimation:shake forKey:@"imageShake"];
//取消动画
//[lpgr.view.layer removeAnimationForKey:@"imageShake"];
}
}
-(void)swipe:(UISwipeGestureRecognizer*)swipeGesture
{
NSLog(@"轻扫,与拖动互斥");
if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"dowm");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"up");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"left");
} else {
NSLog(@"right");
}
}
转场动画
XXXViewController *xvc = [[XXXViewController alloc]init];
CATransition *ani = [CATransition animation];
ani.duration = 0.5f;
//方向
ani.subtype = kCATransitionFromTop;
//四种预设,某些类型中此设置无效
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom
//类型
ani.type = kCATransitionMoveIn;
1.#define定义的常量 (基本型)
kCATransitionFade 交叉淡化过渡
kCATransitionMoveIn 新视图移到旧视图上面
kCATransitionPush 新视图把旧视图推出去
kCATransitionReveal 将旧视图移开,显示下面的新视图
2.用字符串表示
fade moveIn push reveal 和上面的四种一样
pageCurl pageUnCurl 翻页
rippleEffect 滴水效果
suckEffect 收缩效果,如一块布被抽走
cube alignedCube 立方体效果
flip alignedFlip oglFlip 翻转效果
rotate 旋转
cameraIris cameraIrisHollowOpen cameraIrisHollowClose 相机
[self.navigationController.view.layer addAnimation:ani forKey:@"animation"];
[self.navigationController pushViewController:fvc animated:NO];
- (UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect
{//返回image中rect范围内的图片
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *subImage = [UIImage imageWithCGImage:imageRef];
return subImage;
}