[翻译] 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



目录
相关文章
|
Windows
Winform控件优化之背景透明那些事1:Button控件等背景透明
WinForm不支持真正的透明,其控件透明的实现都是背景颜色设置和对应位置的父控件背景相同。 Winform中控件的背景透明只有三种:Button控件的透明、其他控件的透明...
3123 0
Winform控件优化之背景透明那些事1:Button控件等背景透明
|
10月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
913 1
|
XML Android开发 数据格式
Spinner样式大全
关于spinner控件有很多特殊的样式甚至是表现的很夸张的样式,这里就仅仅通过更换系统自带的xml样式来试验各种spinner样式效果。 首先在工程里创建最简单的spinner: 需要更换的地方有两处: 一。
908 0
UICollectionView的布局
UICollectionView的布局
418 0
UICollectionView的布局
|
前端开发 Java Android开发
自定义View——画板
今天实现的是画板效果 image 实现原理 image 根据触摸事件返回的坐标点绘制path路径 @Override public boolean onTouchEvent(MotionEvent event) { x = event.
1031 0