原理:
调用UIView的三个delegate函数(主要正对触摸+滑动操作的回调):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
第一个函数用来记录滑动的起始点;
第二个函数,用来在滑动中,改变UIView的中心点位置,以及缩放UIView;
第三个函数,对于滑动结束后的处理,主要是未滑过分界线和分界线后,UIView自动滑动到预先设定位置。
TouchEaglView.h
- #import <UIKit/UIKit.h>
- @interface TouchEaglView : UIView
- @property (assign, nonatomic) CGPoint beginpoint;
- @property id delegate;
- @end
下面是向右滑动缩小的UIView子类实现:
TouchEagleView.m- //
- // TouchEaglView.m
- // moveView
- //
- // Created by wangdan on 10/1/14.
- // Copyright (c) 2014 wangdan. All rights reserved.
- //
- #import "TouchEaglView.h"
- @implementation TouchEaglView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self)
- {
- // Initialization code
- }
- return self;
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch *touch = [touches anyObject];
- self.beginpoint = [touch locationInView:self];
- [super touchesBegan:touches withEvent:event];
- [super bringSubviewToFront:self];
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- CGPoint point = [[touches anyObject] locationInView:self];
- float dx = point.x - self.beginpoint.x;
- CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y);
- if(self.beginpoint.x<10)
- {
- if(dx>0)
- {
- newcenter.x=MIN(self.bounds.size.width*5/4,newcenter.x);
- self.center=newcenter;
- }
- else
- {
- newcenter.x=MAX(self.superview.bounds.size.width/2,newcenter.x);
- self.center=newcenter;
- }
- [UIView animateWithDuration:0.005 animations:^
- {
- self.transform = CGAffineTransformMakeScale(1-(newcenter.x-160)/1920, 1-(newcenter.x-160)/960);
- }completion:^(BOOL finish)
- {
- }];
- }
- NSLog(@"begin size is %f",self.beginpoint.x);
- }
- // zhege shi chumo jieshu ,huamian zidong tan dao youbian
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if(self.center.x>self.superview.bounds.size.width)
- {
- [UIView animateWithDuration:0.2 animations:^
- {
- self.center=CGPointMake(self.bounds.size.width*5/4, self.center.y);
- self.transform = CGAffineTransformMakeScale(0.875, 0.75);
- }completion:^(BOOL finish)
- {
- }];
- }
- else
- {
- [UIView animateWithDuration:0.2 animations:^
- {
- self.center=CGPointMake(self.bounds.size.width/2,self.center.y);
- self.transform = CGAffineTransformMakeScale(1, 1);
- }completion:^(BOOL finish)
- {
- }];
- }
- }
- @end
下面是滑动后的图片:
上图中左边绿色部分,会随着黄色TouchEagleView滑动而右移并且放大,需要单独实现,原理和上面代码类似