//
// DragLabel.swift
//
// Created by 浩哥 on 2018/4/18.
// Copyright © 2018年 HarrySun. All rights reserved.
//
import UIKit
class DragLabel: UILabel {
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
let touch = touches.first
let current = touch?.location(in: self)
let previous = touch?.previousLocation(in: self)
var center = self.center
guard let currentX = current?.x,
let currentY = current?.y,
let previousX = previous?.x,
let previousY = previous?.y else {
return
}
center.x += CGFloat(currentX - previousX)
center.y += CGFloat(currentY - previousY)
// 控制控件的可拖动范围
if let superview = self.superview, frame.origin.x < superview.bounds.origin.x {
center.x = frame.size.width / 2
}
if let superview = self.superview, frame.origin.x > superview.bounds.size.width - frame.size.width {
center.x = superview.bounds.size.width - frame.size.width / 2
}
if let superview = self.superview, frame.origin.y < superview.bounds.origin.y {
center.y = frame.size.height / 2
}
if let superview = self.superview, frame.origin.y > superview.bounds.size.height - frame.size.height - 20 {
center.y = superview.bounds.size.height - frame.size.height / 2 - 20
}
self.center = center
}
}
可拖动的label
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近一直没有更新简书是因为在开发和测试阶段,有任务,没有进行学习,不过在做任务的时候也遇到了一些技术点,在这里总结...
- 网络让原本需要多层才可以连接起来的关系变的轻松,一部手机在手,你可能与任何人搭上话,便捷的同时,带来的挑战就是信任...