舉例講解iOS開發(fā)中拖動(dòng)視圖的實(shí)現(xiàn)
預(yù)備知識(shí)
iOS處理屏幕上的觸摸動(dòng)作,主要涉及到以下幾個(gè)方法:
touchesBegan:withEvent: //觸摸屏幕的最開始被調(diào)用
touchesMoved:withEvent: //移動(dòng)過程中被調(diào)用
touchesEnded:withEvent: //動(dòng)作結(jié)束時(shí)被調(diào)用
touchesCancelled:WithEvent:
從方法的命名可以清晰的看出該方法何時(shí)被調(diào)用,最后一個(gè)比較特殊。touchesCancelled:WithEvent:在Cocoa Touch必須響應(yīng)持續(xù)觸摸事件的系統(tǒng)中斷時(shí)調(diào)用。
我們只要重寫這些方法,來作我們想要作的事情就可以了。
如何實(shí)現(xiàn)拖動(dòng)視圖?
1.設(shè)置userInteractionEnabled屬性為YES,允許用戶交互。
2.在觸摸動(dòng)作開始時(shí)記錄起始點(diǎn)。
3.在移動(dòng)過程中,計(jì)算當(dāng)前位置坐標(biāo)與起始點(diǎn)的差值,即偏移量,并且移動(dòng)視圖中心點(diǎn)至偏移量大小的地方。
4.分別限制x坐標(biāo)、與y坐標(biāo),保證用戶不可將視圖托出屏幕
備注:分別限制x坐標(biāo)與y坐標(biāo)的原因是,即使向右拖動(dòng)不了了,仍需保證可以向下拖動(dòng)。
其實(shí),功能比較簡(jiǎn)單,就是iOS手勢(shì)動(dòng)畫中的拖動(dòng)。來看一下基本的寫法:
1.注冊(cè)拖動(dòng)動(dòng)畫
UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(doHandlePanAction:)];
[self.vLight addGestureRecognizer:panGestureRecognizer];
注:vLight就是要加入拖動(dòng)的View子類。
2.拖動(dòng)處理函數(shù)
- (void) doHandlePanAction:(UIPanGestureRecognizer *)paramSender{
CGPoint point = [paramSender translationInView:self.view];
NSLog(@"X:%f;Y:%f",point.x,point.y);
paramSender.view.center = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y);
[paramSender setTranslation:CGPointMake(0, 0) inView:self.view];
}
實(shí)現(xiàn)代碼
以子類化UIImageView為例
#import <UIKit/UIKit.h>
@interface GragView : UIImageView
{
CGPoint startPoint;
}
@end
#import "GragView.h"
@implementation GragView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//允許用戶交互
self.userInteractionEnabled = YES;
}
return self;
}
- (id)initWithImage:(UIImage *)image
{
self = [super initWithImage:image];
if (self) {
//允許用戶交互
self.userInteractionEnabled = YES;
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//保存觸摸起始點(diǎn)位置
CGPoint point = [[touches anyObject] locationInView:self];
startPoint = point;
//該view置于最前
[[self superview] bringSubviewToFront:self];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//計(jì)算位移=當(dāng)前位置-起始位置
CGPoint point = [[touches anyObject] locationInView:self];
float dx = point.x - startPoint.x;
float dy = point.y - startPoint.y;
//計(jì)算移動(dòng)后的view中心點(diǎn)
CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
/* 限制用戶不可將視圖托出屏幕 */
float halfx = CGRectGetMidX(self.bounds);
//x坐標(biāo)左邊界
newcenter.x = MAX(halfx, newcenter.x);
//x坐標(biāo)右邊界
newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
//y坐標(biāo)同理
float halfy = CGRectGetMidY(self.bounds);
newcenter.y = MAX(halfy, newcenter.y);
newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
//移動(dòng)view
self.center = newcenter;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
相關(guān)文章
iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01fastlane自動(dòng)化打包iOS APP過程示例
這篇文章主要為大家介紹了fastlane自動(dòng)化打包iOS APP的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07iOS實(shí)現(xiàn)通過按鈕添加和刪除控件的方法
這篇文章主要為大家詳細(xì)介紹了iOS通過按鈕添加和刪除控件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03iOS實(shí)現(xiàn)應(yīng)用內(nèi)切換本地化語言的方法實(shí)例
網(wǎng)絡(luò)上關(guān)于iOS國際化的文章很多,但基本上都是基于跟隨系統(tǒng)語言的國際化,而這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)應(yīng)用內(nèi)切換本地化語言的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考。2017-12-12實(shí)例講解設(shè)計(jì)模式中的命令模式在iOS App開發(fā)中的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式中的命令模式在iOS App開發(fā)中的運(yùn)用,文中還講到了Cocoa框架下使用的例子,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03簡(jiǎn)單介紹iOS開發(fā)中關(guān)于category的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中關(guān)于category的應(yīng)用,代碼仍然基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09iOS中UIWebView網(wǎng)頁加載組件的基礎(chǔ)及使用技巧實(shí)例
UIWebView是開發(fā)中很常用的應(yīng)用內(nèi)調(diào)用網(wǎng)頁瀏覽的控件,這里整理了一些iOS中UIWebView網(wǎng)頁加載組件的基礎(chǔ)及使用技巧實(shí)例 ,需要的朋友可以參考下2016-06-06