美文网首页iOS点点滴滴
仿iPhone-AssistiveTouch

仿iPhone-AssistiveTouch

作者: NN_逝去 | 来源:发表于2017-11-08 16:17 被阅读19次
拖拽效果.gif

UIView的触摸事件主要有:文字来源
一根或者多根⼿手指开始触摸view,系统会⾃自动调⽤用view的下⾯面⽅方法.

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    一根或者多根⼿手指在view上移动时,系统会⾃自动调⽤用view的下⾯面⽅方法 (随着⼿手指的移动,会持续调⽤用该⽅方法,也就是说这个⽅方法会调⽤用很多次)
  • (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    一根或者多根⼿手指离开view,系统会⾃自动调⽤用view的下⾯面⽅方法
  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    一当突然发生系统事件的时候,例如突然来了系统电话,或是手机没电的时候,系统就会自动调用
  • (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

代码事例(被抛弃的小功能)

// .h文件
#import <UIKit/UIKit.h>

@interface MXDragEnableView : UIImageView

@end

// .m文件
#import "MXDragEnableView.h"
#define PADDING     5

@interface MXDragEnableView () {
    CGPoint beginPoint;
}

@end
@implementation MXDragEnableView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    beginPoint = [touch locationInView:self];
    [[self superview] bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint nowPoint = [touch locationInView:self];
    float offsetX = nowPoint.x - beginPoint.x;
    float offsetY = nowPoint.y - beginPoint.y;
    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setNewDraggedViewFrame];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setNewDraggedViewFrame];
}

- (void)setNewDraggedViewFrame
{
    float marginLeft = self.frame.origin.x;
    float marginRight = self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;
    float marginTop = self.frame.origin.y;
    float marginBottom = self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height;
    
    //    marginLeft<marginRight?PADDING:
    if (marginTop<98) {
        self.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.frame.size.width - self.frame.size.width-PADDING,
                                PADDING,
                                self.frame.size.width,
                                self.frame.size.height);
    }
    else if (marginBottom<83) {
        self.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.frame.size.width - self.frame.size.width-PADDING,
                                self.superview.frame.size.height - self.frame.size.height-PADDING,
                                self.frame.size.width,
                                self.frame.size.height);
        
    }
    else {
        self.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.frame.size.width - self.frame.size.width-PADDING,
                                self.frame.origin.y,
                                self.frame.size.width,
                                self.frame.size.height);
    }

}

@end 

相关文章

  • 仿iPhone-AssistiveTouch

    UIView的触摸事件主要有:文字来源一根或者多根⼿手指开始触摸view,系统会⾃自动调⽤用view的下⾯面⽅方法...

  • 仿写JQ方法

    原生js仿写jq 仿写jq的click方法 仿写jq的on事件 仿写jq的eq方法 仿写jq的 css方法 仿写j...

  • ​汉语修辞学摘要(二)

    一 仿拟 1.仿拟的结构类型 (1)仿词语 (2)仿句子 (3)仿语篇 2.仿拟的修辞功用 (1)增添语言的幽默情...

  • 歌词|仿〈随听 于文文•体面 有感〉

    仿 仿你喜欢的歌手唱 仿你喜欢的人练臂膀 仿你喜欢的一切治愈疗伤 仿熬汤的每一个细节 仿闭上眼睛的每一次睫毛眨 仿...

  • html5仿微信界面|h5仿微信聊天实战项目

    html5+css3高仿微信聊天|h5仿微信界面|仿微信朋友圈|仿微信红包|h5仿微信支付键盘|h5仿微信群聊 近...

  • 古诗词创作技巧目录

    一 初学仿写 1.1引言 1.2初学仿写之咏鹅 1.3初学仿写之静夜思 1.4初学仿写之出塞 1.5 初学仿写之天...

  • 古诗词创作技巧总目录

    第一章 初级仿写 1.1引言 1.2初学仿写之咏鹅 1.3初学仿写之静夜思 1.4初学仿写之出塞 1.5 初学仿写...

  • 仿砂岩石漆是什么?仿石漆是什么?还傻傻分不清

    我们常说的仿石漆有很多种类,包括真石漆、水包水多彩漆、水包砂多彩漆,仿砂岩漆,仿大理石漆,仿风化石漆,仿法国木纹石...

  • 学习仿写

    学习 目标 ①研读优秀作品,确定仿写要点。 ②根据仿写要求,学会片段仿写。③学会融会贯通,进行全文仿写。 1导课。...

  • 学习仿写

    学习 目标 ①研读优秀作品,确定仿写要点。 ②根据仿写要求,学会片段仿写。③学会融会贯通,进行全文仿写。 1导课。...

网友评论

    本文标题:仿iPhone-AssistiveTouch

    本文链接:https://www.haomeiwen.com/subject/plcymxtx.html