平移视图效果

本文介绍了一个名为CLTranslationView的iOS视图组件,该组件通过处理用户的手势输入来支持视图的拖动操作。文章详细展示了如何通过Objective-C语言实现多点触控的支持,并解释了在不同手势阶段(开始、移动、结束和取消)如何更新视图的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  CLTranslationView.m
//  LessonUIEvent
//
//  Created by lanouhn on 14-8-25.
//  Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.
//

#import "CLTranslationView.h"

@interface CLTranslationView ()
{
    CGPoint _previousPoint;//存储移动之前的点的位置
}
@end


@implementation CLTranslationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //用来设置当前视图是否支持多点触摸. iOS虽然支持多次触摸, 但是默认的是单点触摸
        self.multipleTouchEnabled = YES;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%lu", (unsigned long)touches.count);
    UITouch *touch = [touches anyObject];
    //获取手指触摸在视图上的位置
    _previousPoint = [touch locationInView:self];
    //膨胀检测 有大到小
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取手指对象
    UITouch *touch = [touches anyObject];
    //获取移动之后手指在视图上的位置
    CGPoint currentPoint = [touch locationInView:self];
    //获取移动之前和移动之后的坐标变化量
    CGFloat dx = currentPoint.x - _previousPoint.x;
    CGFloat dy = currentPoint.y - _previousPoint.y;
    self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值