zoukankan      html  css  js  c++  java
  • [翻译] UIView-draggable 可拖拽的UIView

    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){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
  • 相关阅读:
    two pointers思想 ---- 利用两个i, j两个下标,同时对序列进行扫描,以O(n)复杂度解决问题的一种思想
    二分法
    区间贪心
    error C2825: '_Iter': 当后面跟“::”时必须为类或命名空间 -- 原因可能是参数错误或者自定义函数名和库函数名冲突
    模态窗口的定时关闭
    数据结构(二十二)二叉树遍历算法的应用与二叉树的建立
    数据结构(二十一)二叉树的非递归遍历算法
    数据结构(二十)二叉树的递归遍历算法
    数据结构(十九)二叉树的定义和性质
    数据结构(十八)树的定义与存储结构
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3674656.html
Copyright © 2011-2022 走看看