create
This commit is contained in:
17
ShenQi/WMDragView/DSAvatarCollectionViewCell.h
Normal file
17
ShenQi/WMDragView/DSAvatarCollectionViewCell.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// DSAvatarCollectionViewCell.h
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DSAvatarCollectionViewCell : UICollectionViewCell
|
||||
|
||||
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
16
ShenQi/WMDragView/DSAvatarCollectionViewCell.m
Normal file
16
ShenQi/WMDragView/DSAvatarCollectionViewCell.m
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// DSAvatarCollectionViewCell.m
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import "DSAvatarCollectionViewCell.h"
|
||||
|
||||
@implementation DSAvatarCollectionViewCell
|
||||
|
||||
- (void)awakeFromNib {
|
||||
[super awakeFromNib];
|
||||
// Initialization code
|
||||
}
|
||||
|
||||
@end
|
||||
39
ShenQi/WMDragView/DSAvatarCollectionViewCell.xib
Normal file
39
ShenQi/WMDragView/DSAvatarCollectionViewCell.xib
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="22155" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait" appearance="light"/>
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22131"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="gTV-IL-0wX" customClass="DSAvatarCollectionViewCell">
|
||||
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
|
||||
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<subviews>
|
||||
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="menu" translatesAutoresizingMaskIntoConstraints="NO" id="RqP-X3-yqZ">
|
||||
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
|
||||
</imageView>
|
||||
</subviews>
|
||||
</view>
|
||||
<constraints>
|
||||
<constraint firstItem="RqP-X3-yqZ" firstAttribute="centerX" secondItem="gTV-IL-0wX" secondAttribute="centerX" id="5mY-C8-5lu"/>
|
||||
<constraint firstItem="RqP-X3-yqZ" firstAttribute="centerY" secondItem="gTV-IL-0wX" secondAttribute="centerY" id="SJv-lH-lTa"/>
|
||||
<constraint firstItem="RqP-X3-yqZ" firstAttribute="height" secondItem="gTV-IL-0wX" secondAttribute="height" id="x5u-MZ-CFE"/>
|
||||
<constraint firstItem="RqP-X3-yqZ" firstAttribute="width" secondItem="gTV-IL-0wX" secondAttribute="width" id="yXf-Uz-mdk"/>
|
||||
</constraints>
|
||||
<connections>
|
||||
<outlet property="avatarImageView" destination="RqP-X3-yqZ" id="Ltx-1Y-jla"/>
|
||||
</connections>
|
||||
<point key="canvasLocation" x="137.68115942028987" y="125.89285714285714"/>
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="menu" width="574" height="574"/>
|
||||
</resources>
|
||||
</document>
|
||||
17
ShenQi/WMDragView/DSWebDragView.h
Normal file
17
ShenQi/WMDragView/DSWebDragView.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// DSWebDragView.h
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import "WMDragView.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DSWebDragView : WMDragView
|
||||
|
||||
@property (strong, nonatomic) UIImageView *homeImageView;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
43
ShenQi/WMDragView/DSWebDragView.m
Normal file
43
ShenQi/WMDragView/DSWebDragView.m
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// DSWebDragView.m
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import "DSWebDragView.h"
|
||||
|
||||
@implementation DSWebDragView
|
||||
|
||||
-(void)awakeFromNib
|
||||
{
|
||||
[super awakeFromNib];
|
||||
|
||||
self.userInteractionEnabled = YES;
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
|
||||
self.homeImageView = [[UIImageView alloc] initWithFrame:self.bounds];
|
||||
self.homeImageView.image = [UIImage imageNamed:@"menu"];
|
||||
self.homeImageView.backgroundColor = [UIColor whiteColor];
|
||||
self.homeImageView.layer.cornerRadius = 30.f;
|
||||
self.homeImageView.layer.masksToBounds = YES;
|
||||
[self addSubview:self.homeImageView];
|
||||
}
|
||||
|
||||
-(instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
self.userInteractionEnabled = YES;
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
|
||||
self.homeImageView = [[UIImageView alloc] initWithFrame:self.bounds];
|
||||
self.homeImageView.image = [UIImage imageNamed:@"menu"];
|
||||
self.homeImageView.backgroundColor = [UIColor whiteColor];
|
||||
self.homeImageView.layer.cornerRadius = 30.f;
|
||||
self.homeImageView.layer.masksToBounds = YES;
|
||||
[self addSubview:self.homeImageView];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
32
ShenQi/WMDragView/DSWebMenuCollectionView.h
Normal file
32
ShenQi/WMDragView/DSWebMenuCollectionView.h
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// DSWebMenuCollectionView.h
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef NS_ENUM(NSInteger, DSWebMenuType) {
|
||||
DSWebMenuHomeType, // 主页
|
||||
DSWebMenuTelegramType, // Telegram
|
||||
DSWebMenuWhatsAppType, // WhatsApp
|
||||
DSWebMenuFacebookType, // Facebook
|
||||
};
|
||||
|
||||
@protocol DSWebMenuDelegate <NSObject>
|
||||
|
||||
-(void)webMenuCollectionViewWithMenuType:(DSWebMenuType)webMenuType;
|
||||
|
||||
@end
|
||||
|
||||
@interface DSWebMenuCollectionView : UICollectionView <UICollectionViewDelegate,UICollectionViewDataSource>
|
||||
|
||||
@property (nonatomic, weak) id<DSWebMenuDelegate> webMenuDelegate;
|
||||
|
||||
@property (nonatomic, strong) NSMutableArray <NSNumber *>*shareArray;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
102
ShenQi/WMDragView/DSWebMenuCollectionView.m
Normal file
102
ShenQi/WMDragView/DSWebMenuCollectionView.m
Normal file
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// DSWebMenuCollectionView.m
|
||||
// VietnamGame
|
||||
//
|
||||
//
|
||||
|
||||
#import "DSWebMenuCollectionView.h"
|
||||
|
||||
#import "DSAvatarCollectionViewCell.h"
|
||||
|
||||
static NSString *identifier = @"AvatarCollectionViewCell";
|
||||
|
||||
@implementation DSWebMenuCollectionView
|
||||
|
||||
-(void)awakeFromNib
|
||||
{
|
||||
[super awakeFromNib];
|
||||
|
||||
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
|
||||
flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
|
||||
self.collectionViewLayout = flowlayout;
|
||||
self.delegate = self;
|
||||
self.dataSource = self;
|
||||
self.showsVerticalScrollIndicator = NO;
|
||||
self.showsHorizontalScrollIndicator = NO;
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
[self registerNib:[UINib nibWithNibName:@"DSAvatarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];
|
||||
[self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier];
|
||||
}
|
||||
|
||||
-(void)setShareArray:(NSMutableArray<NSNumber *> *)shareArray
|
||||
{
|
||||
_shareArray = shareArray;
|
||||
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
#pragma mark - 推荐
|
||||
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
||||
{
|
||||
return self.shareArray.count;
|
||||
}
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return CGSizeMake(collectionView.frame.size.width, ((collectionView.frame.size.height-(self.shareArray.count-1)*10))/self.shareArray.count);
|
||||
}
|
||||
|
||||
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
|
||||
{
|
||||
return UIEdgeInsetsMake(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier forIndexPath:indexPath];
|
||||
return view;
|
||||
}
|
||||
|
||||
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
DSAvatarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
|
||||
NSNumber *number = self.shareArray[indexPath.row];
|
||||
DSWebMenuType type = number.integerValue;
|
||||
if (type == DSWebMenuHomeType) {
|
||||
cell.avatarImageView.image = [UIImage imageNamed:@"zhongxindakai"];
|
||||
}else if (type == DSWebMenuTelegramType) {
|
||||
cell.avatarImageView.image = [UIImage imageNamed:@"telegram"];
|
||||
}else if (type == DSWebMenuWhatsAppType) {
|
||||
cell.avatarImageView.image = [UIImage imageNamed:@"whatsapp"];
|
||||
}else if (type == DSWebMenuFacebookType) {
|
||||
cell.avatarImageView.image = [UIImage imageNamed:@"facebook"];
|
||||
}
|
||||
cell.avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
return cell;
|
||||
}
|
||||
|
||||
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
NSNumber *number = self.shareArray[indexPath.row];
|
||||
DSWebMenuType type = number.integerValue;
|
||||
if (self.webMenuDelegate && [self.webMenuDelegate respondsToSelector:@selector(webMenuCollectionViewWithMenuType:)]) {
|
||||
[self.webMenuDelegate webMenuCollectionViewWithMenuType:type];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
75
ShenQi/WMDragView/WMDragView.h
Normal file
75
ShenQi/WMDragView/WMDragView.h
Normal file
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// WMDragView.h
|
||||
// WMDragView
|
||||
//
|
||||
// Created by zhengwenming on 2016/12/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
|
||||
// 拖曳view的方向
|
||||
typedef NS_ENUM(NSInteger, WMDragDirection) {
|
||||
WMDragDirectionAny, /**< 任意方向 */
|
||||
WMDragDirectionHorizontal, /**< 水平方向 */
|
||||
WMDragDirectionVertical, /**< 垂直方向 */
|
||||
};
|
||||
|
||||
@interface WMDragView : UIView
|
||||
/**
|
||||
是不是能拖曳,默认为YES
|
||||
YES,能拖曳
|
||||
NO,不能拖曳
|
||||
*/
|
||||
@property (nonatomic,assign) BOOL dragEnable;
|
||||
|
||||
/**
|
||||
活动范围,默认为父视图的frame范围内(因为拖出父视图后无法点击,也没意义)
|
||||
如果设置了,则会在给定的范围内活动
|
||||
如果没设置,则会在父视图范围内活动
|
||||
注意:设置的frame不要大于父视图范围
|
||||
注意:设置的frame为0,0,0,0表示活动的范围为默认的父视图frame,如果想要不能活动,请设置dragEnable这个属性为NO
|
||||
*/
|
||||
@property (nonatomic,assign) CGRect freeRect;
|
||||
|
||||
/**
|
||||
拖曳的方向,默认为any,任意方向
|
||||
*/
|
||||
@property (nonatomic,assign) WMDragDirection dragDirection;
|
||||
|
||||
/**
|
||||
contentView内部懒加载的一个UIImageView
|
||||
开发者也可以自定义控件添加到本view中
|
||||
注意:最好不要同时使用内部的imageView和button
|
||||
*/
|
||||
@property (nonatomic,strong) UIImageView *imageView;
|
||||
/**
|
||||
contentView内部懒加载的一个UIButton
|
||||
开发者也可以自定义控件添加到本view中
|
||||
注意:最好不要同时使用内部的imageView和button
|
||||
*/
|
||||
@property (nonatomic,strong) UIButton *button;
|
||||
/**
|
||||
是不是总保持在父视图边界,默认为NO,没有黏贴边界效果
|
||||
isKeepBounds = YES,它将自动黏贴边界,而且是最近的边界
|
||||
isKeepBounds = NO, 它将不会黏贴在边界,它是free(自由)状态,跟随手指到任意位置,但是也不可以拖出给定的范围frame
|
||||
*/
|
||||
@property (nonatomic,assign) BOOL isKeepBounds;
|
||||
/**
|
||||
点击的回调block
|
||||
*/
|
||||
@property (nonatomic,copy) void(^clickDragViewBlock)(WMDragView *dragView);
|
||||
/**
|
||||
开始拖动的回调block
|
||||
*/
|
||||
@property (nonatomic,copy) void(^beginDragBlock)(WMDragView *dragView);
|
||||
/**
|
||||
拖动中的回调block
|
||||
*/
|
||||
@property (nonatomic,copy) void(^duringDragBlock)(WMDragView *dragView);
|
||||
/**
|
||||
结束拖动的回调block
|
||||
*/
|
||||
@property (nonatomic,copy) void(^endDragBlock)(WMDragView *dragView);
|
||||
@end
|
||||
227
ShenQi/WMDragView/WMDragView.m
Normal file
227
ShenQi/WMDragView/WMDragView.m
Normal file
@@ -0,0 +1,227 @@
|
||||
//
|
||||
// WMDragView.m
|
||||
// WMDragView
|
||||
//
|
||||
// Created by zhengwenming on 2016/12/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "WMDragView.h"
|
||||
|
||||
@interface WMDragView ()<UIGestureRecognizerDelegate>
|
||||
@property (nonatomic,strong) UIView *contentViewForDrag;
|
||||
|
||||
/**
|
||||
内容view,命名为contentViewForDrag,因为很多其他开源的第三方的库,里面同样有contentView这个属性
|
||||
,这里特意命名为contentViewForDrag以防止冲突
|
||||
*/
|
||||
@property (nonatomic,assign) CGPoint startPoint;
|
||||
@property (nonatomic,strong) UIPanGestureRecognizer *panGestureRecognizer;
|
||||
@property (nonatomic,assign) CGFloat previousScale;
|
||||
@end
|
||||
|
||||
@implementation WMDragView
|
||||
-(UIImageView *)imageView{
|
||||
if (_imageView==nil) {
|
||||
_imageView = [[UIImageView alloc]init];
|
||||
_imageView.userInteractionEnabled = YES;
|
||||
_imageView.clipsToBounds = YES;
|
||||
[self.contentViewForDrag addSubview:_imageView];
|
||||
}
|
||||
return _imageView;
|
||||
}
|
||||
-(UIButton *)button{
|
||||
if (_button==nil) {
|
||||
_button = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
_button.clipsToBounds = YES;
|
||||
_button.userInteractionEnabled = NO;
|
||||
[self.contentViewForDrag addSubview:_button];
|
||||
}
|
||||
return _button;
|
||||
}
|
||||
-(UIView *)contentViewForDrag{
|
||||
if (_contentViewForDrag==nil) {
|
||||
_contentViewForDrag = [[UIView alloc]init];
|
||||
_contentViewForDrag.clipsToBounds = YES;
|
||||
}
|
||||
return _contentViewForDrag;
|
||||
}
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
[self addSubview:self.contentViewForDrag];
|
||||
[self setUp];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (instancetype)initWithCoder:(NSCoder *)coder
|
||||
{
|
||||
self = [super initWithCoder:coder];
|
||||
if (self) {
|
||||
[self setUp];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void)layoutSubviews{
|
||||
[super layoutSubviews];
|
||||
if (self.freeRect.origin.x!=0||self.freeRect.origin.y!=0||self.freeRect.size.height!=0||self.freeRect.size.width!=0) {
|
||||
//设置了freeRect--活动范围
|
||||
}else{
|
||||
//没有设置freeRect--活动范围,则设置默认的活动范围为父视图的frame
|
||||
self.freeRect = (CGRect){CGPointZero,self.superview.bounds.size};
|
||||
}
|
||||
_imageView.frame = (CGRect){CGPointZero,self.bounds.size};
|
||||
_button.frame = (CGRect){CGPointZero,self.bounds.size};
|
||||
self.contentViewForDrag.frame = (CGRect){CGPointZero,self.bounds.size};
|
||||
}
|
||||
-(void)setUp{
|
||||
self.dragEnable = YES;//默认可以拖曳
|
||||
self.clipsToBounds = YES;
|
||||
self.isKeepBounds = NO;
|
||||
self.backgroundColor = [UIColor lightGrayColor];
|
||||
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickDragView)];
|
||||
[self addGestureRecognizer:singleTap];
|
||||
|
||||
//添加移动手势可以拖动
|
||||
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragAction:)];
|
||||
self.panGestureRecognizer.minimumNumberOfTouches = 1;
|
||||
self.panGestureRecognizer.maximumNumberOfTouches = 1;
|
||||
self.panGestureRecognizer.delegate = self;
|
||||
[self addGestureRecognizer:self.panGestureRecognizer];
|
||||
}
|
||||
//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
|
||||
// return self.dragEnable;
|
||||
//}
|
||||
/**
|
||||
拖动事件
|
||||
@param pan 拖动手势
|
||||
*/
|
||||
-(void)dragAction:(UIPanGestureRecognizer *)pan{
|
||||
if(self.dragEnable==NO)return;
|
||||
switch (pan.state) {
|
||||
case UIGestureRecognizerStateBegan:{//开始拖动
|
||||
if (self.beginDragBlock) {
|
||||
self.beginDragBlock(self);
|
||||
}
|
||||
//注意完成移动后,将translation重置为0十分重要。否则translation每次都会叠加
|
||||
[pan setTranslation:CGPointZero inView:self];
|
||||
//保存触摸起始点位置
|
||||
self.startPoint = [pan translationInView:self];
|
||||
break;
|
||||
}
|
||||
case UIGestureRecognizerStateChanged:{//拖动中
|
||||
//计算位移 = 当前位置 - 起始位置
|
||||
if (self.duringDragBlock) {
|
||||
self.duringDragBlock(self);
|
||||
}
|
||||
CGPoint point = [pan translationInView:self];
|
||||
float dx;
|
||||
float dy;
|
||||
switch (self.dragDirection) {
|
||||
case WMDragDirectionAny:
|
||||
dx = point.x - self.startPoint.x;
|
||||
dy = point.y - self.startPoint.y;
|
||||
break;
|
||||
case WMDragDirectionHorizontal:
|
||||
dx = point.x - self.startPoint.x;
|
||||
dy = 0;
|
||||
break;
|
||||
case WMDragDirectionVertical:
|
||||
dx = 0;
|
||||
dy = point.y - self.startPoint.y;
|
||||
break;
|
||||
default:
|
||||
dx = point.x - self.startPoint.x;
|
||||
dy = point.y - self.startPoint.y;
|
||||
break;
|
||||
}
|
||||
|
||||
//计算移动后的view中心点
|
||||
CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy);
|
||||
//移动view
|
||||
self.center = newCenter;
|
||||
// 注意完成上述移动后,将translation重置为0十分重要。否则translation每次都会叠加
|
||||
[pan setTranslation:CGPointZero inView:self];
|
||||
break;
|
||||
}
|
||||
case UIGestureRecognizerStateEnded:{//拖动结束
|
||||
[self keepBounds];
|
||||
if (self.endDragBlock) {
|
||||
self.endDragBlock(self);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
//点击事件
|
||||
-(void)clickDragView{
|
||||
if (self.clickDragViewBlock) {
|
||||
self.clickDragViewBlock(self);
|
||||
}
|
||||
}
|
||||
//黏贴边界效果
|
||||
- (void)keepBounds{
|
||||
//中心点判断
|
||||
float centerX = self.freeRect.origin.x+(self.freeRect.size.width - self.frame.size.width)/2;
|
||||
CGRect rect = self.frame;
|
||||
if (self.isKeepBounds==NO) {//没有黏贴边界的效果
|
||||
if (self.frame.origin.x < self.freeRect.origin.x) {
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"leftMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.x = self.freeRect.origin.x;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
} else if(self.freeRect.origin.x+self.freeRect.size.width < self.frame.origin.x+self.frame.size.width){
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"rightMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.x = self.freeRect.origin.x+self.freeRect.size.width-self.frame.size.width;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
}
|
||||
}else if(self.isKeepBounds==YES){//自动粘边
|
||||
if (self.frame.origin.x< centerX) {
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"leftMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.x = self.freeRect.origin.x;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
} else {
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"rightMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.x =self.freeRect.origin.x+self.freeRect.size.width - self.frame.size.width;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
}
|
||||
}
|
||||
|
||||
if (self.frame.origin.y < self.freeRect.origin.y) {
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"topMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.y = self.freeRect.origin.y;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
} else if(self.freeRect.origin.y+self.freeRect.size.height< self.frame.origin.y+self.frame.size.height){
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
[UIView beginAnimations:@"bottomMove" context:context];
|
||||
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
||||
[UIView setAnimationDuration:0.5];
|
||||
rect.origin.y = self.freeRect.origin.y+self.freeRect.size.height-self.frame.size.height;
|
||||
self.frame = rect;
|
||||
[UIView commitAnimations];
|
||||
}
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user