博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[翻译] UIView-draggable 可拖拽的UIView
阅读量:4618 次
发布时间:2019-06-09

本文共 3235 字,大约阅读时间需要 10 分钟。

UIView-draggable 可拖拽的UIView

UIView category that adds dragging capabilities

一个类目用来给UIView便利的添加拖拽手势

Setup with Cocoapods 用Cocoapods设置

  • Add pod 'UIView+draggable' to your Podfile
  • Run pod install
  • Run open App.xcworkspace
  • Import UIVIew+draggable.h in your controller's header file

Usage 使用

// Enable dragging[self.view enableDragging];

TODO

  • Write the README :)

 

UIView+draggable.h

////  UIView+draggable.h//  UIView+draggable////  Created by Andrea on 13/03/14.//  Copyright (c) 2014 Fancy Pixel. All rights reserved.//@interface UIView (draggable)/**----------------------------------------------------------------------------- * @name UIView+draggable Properties * ----------------------------------------------------------------------------- *//** The pan gestures that handles the view dragging * * @param panGesture The tint color of the blurred view. Set to nil to reset. */@property (nonatomic) UIPanGestureRecognizer *panGesture;/**----------------------------------------------------------------------------- * @name UIView+draggable Methods * ----------------------------------------------------------------------------- *//** Enables the dragging * * Enables the dragging state of the view */- (void)enableDragging;/** Disable or enable the view dragging * * @param draggable The boolean that enables or disables the draggable state */- (void)setDraggable:(BOOL)draggable;@end

UIView+draggable.m

////  UIView+draggable.m//  UIView+draggable////  Created by Andrea on 13/03/14.//  Copyright (c) 2014 Fancy Pixel. All rights reserved.//#import "UIView+draggable.h"#import 
@implementation UIView (draggable)- (void)setPanGesture:(UIPanGestureRecognizer*)panGesture{ objc_setAssociatedObject(self, @selector(panGesture), panGesture, OBJC_ASSOCIATION_RETAIN);}- (UIPanGestureRecognizer*)panGesture{ return objc_getAssociatedObject(self, @selector(panGesture));}- (void)handlePan:(UIPanGestureRecognizer*)sender{ [self adjustAnchorPointForGestureRecognizer:sender]; CGPoint translation = [sender translationInView:[self superview]]; [self setCenter:CGPointMake([self center].x + translation.x, [self center].y + translation.y)]; [sender setTranslation:(CGPoint){
0, 0} inView:[self superview]];}- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { UIView *piece = self; CGPoint locationInView = [gestureRecognizer locationInView:piece]; CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview]; piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height); piece.center = locationInSuperview; }}- (void)setDraggable:(BOOL)draggable{ [self.panGesture setEnabled:draggable];}- (void)enableDragging{ self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [self.panGesture setMaximumNumberOfTouches:1]; [self.panGesture setMinimumNumberOfTouches:1]; [self.panGesture setCancelsTouchesInView:NO]; [self addGestureRecognizer:self.panGesture];}@end

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/YouXianMing/p/3674656.html

你可能感兴趣的文章
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>
关于微信公众平台测试号配置失败的问题
查看>>
【NOIP2001】统计单词个数
查看>>
linux常用端口
查看>>
异常处理
查看>>
/proc/uptime详解
查看>>
如何建立合适的索引?
查看>>
acwing 651. 逛画展
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
python的沙盒环境--virtualenv
查看>>