IOS 实现边滑动边缩放的类似qq主界面的页面切换功能

原理:

调用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 显示


下面是滑动后的图片:



上图中左边绿色部分,会随着黄色TouchEagleView滑动而右移并且放大,需要单独实现,原理和上面代码类似

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值