UIView-draggable 可拖拽的UIView

https://github.com/andreamazz/UIView-draggable

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 <objc/runtime.h> @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){, } 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:];
[self.panGesture setMinimumNumberOfTouches:];
[self.panGesture setCancelsTouchesInView:NO];
[self addGestureRecognizer:self.panGesture];
} @end

[翻译] UIView-draggable 可拖拽的UIView的更多相关文章

  1. iOS边练边学--触摸事件以及能够拖拽的UIView的练习

    一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...

  2. Vue.Draggable实现拖拽效果(采坑小记)

    之前有写过Vue.Draggable实现拖拽效果(快速使用)(http://www.cnblogs.com/songdongdong/p/6928945.html)最近项目中要用到这个拖拽的效果,当产 ...

  3. flutter Draggable Widget拖拽控件

    Draggable Widget Draggable控件负责就是拖拽,父层使用了Draggable,它的子元素就是可以拖动的,子元素可以实容器,可以是图片.用起来非常的灵活. 参数说明: data: ...

  4. vue draggable 火狐拖拽搜索问题

    最近在使用vuedraggable做导航时候,谷歌拖拽是没问题的,但是在火狐测试时候,拖拽时候是可以成功,但是火狐还是打开了一个新的tab,并且搜索了,一开始想着是阻止默认行为,但是在@end时间中阻 ...

  5. Vue.Draggable实现拖拽效果(快速使用)

    1.下载包:npm install vuedraggable 配置:package.json "dependencies": { "element-ui": & ...

  6. JQUERY 拖拽 draggable droppable resizable selectable sortable

    今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下:   <script language="JavaScript" type="text/ ...

  7. jQuery UI API - 可拖拽小部件(Draggable Widget)(转)

    所属类别 交互(Interactions) 用法 描述:允许使用鼠标移动元素. 版本新增:1.0 依赖: UI 核心(UI Core) 部件库(Widget Factory) 鼠标交互(Mouse I ...

  8. jquery 实现页面拖拽并保存到cookie

    实现的效果就是页面内的图片可拖拽到任意位置,并将所在位置保存.下次打开页面依然可见.本文是作demo用,实际开发中,位置的数据应保存到数据库中. 好了,开始. 1.准备工作. a.jquery(1.7 ...

  9. sortable.js 拖拽排序及配置项说明

    // 拖动排序 $(function() { /*排序*/ //排序 // Simple list ]; new Sortable(list, { group: "name", a ...

随机推荐

  1. C++的黑科技(深入探索C++对象模型)

    周二面了腾讯,之前只投了TST内推,貌似就是TST面试了 其中有一个问题,“如何产生一个不能被继承的类”,这道题我反反复复只想到,将父类的构造函数私有,让子类不能调用,最后归结出一个单例模式,但面试官 ...

  2. Reflow(回流)和Repaint(重绘) (转)

    原文地址:http://blog.csdn.net/qq_18826911/article/details/68924255 首先我们要明白的是,页面的显示过程分为以下几个阶段: 1.生成DOM树(包 ...

  3. git与pycharm合并,珠联璧合

    前段时间提交代码都是各种手写push push push,好蠢,今天尝试了一下ide直接提交,爽的一匹,做个总结. 首先github帐号肯定要有. 看图,设置好帐号啥的 再设置git路径,这个应该自动 ...

  4. js获取或设置当前窗口url参数

    直接上代码 // 获取当前窗口url中param参数的值 function get_param(param){ var query = location.search.substring(1).spl ...

  5. Good Bye 2015 F - New Year and Cleaning

    F - New Year and Cleaning 这题简直是丧心病狂折磨王.. 思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列. 为了优化找到下一个消去的点,我先 ...

  6. C++的一道变态题

    题目大概是这样的:有两个数组a[N],b[N],求构造 b[i]=a[0]*a[1]*a[2]*...a[N-1]/a[i], 要求: .不能使用除法. .空间复杂度O(1),时间复杂度O(n). . ...

  7. 如果修改GeneXus Android的一些源码文件(FlexibleClient)

    在使用GeneXus开发Android应用的过程中遇到了一个问题,使用tabs控件时发现默认高度过高,和UI设计要求的高度不一致,找了很久发现没有地方设置.后来联系了GeneXus中国厂商,得到了答复 ...

  8. bzoj1093 [ZJOI2007]最大半联通子图 缩点 + 拓扑序

    最大半联通子图对应缩点后的$DAG$上的最长链 复杂度$O(n + m)$ #include <cstdio> #include <cstring> #include < ...

  9. [POJ1205]Water Treatment Plants

    题目大意: 有一排n个格子,要在它们上面装管道. 每个格子上的管道都是T形的,但是可以有三种流动的方向. 每种都是把两个方向的水往另一个方向排出. 如果方向是向左或向右,就是排到相邻的格子里. 特别地 ...

  10. 修改选择排序 Exercise07_20

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:修改选择排序 * */ public class Exercise07_2 ...