iOS UIGesutreRecognizer&實(shí)現(xiàn)抽屜效果

抽屜效果

實(shí)現(xiàn):

1.界面切換
2.界面復(fù)位
3.界面自動(dòng)轉(zhuǎn)換

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViewUp];
    //添加手勢(shì)
    UIPanGestureRecognizer *panGs = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGs:)];
    [self.redView addGestureRecognizer:panGs];
    //添加監(jiān)聽(tīng)手勢(shì)復(fù)位
    [self addTapGesture];
}
//添加手勢(shì)
- (void)addTapGesture{
    UITapGestureRecognizer *tapGs = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tapGs];
}
- (void)tapAction{
    [UIView animateWithDuration:0.7 animations:^{
        self.redView.frame = [[UIScreen mainScreen] bounds];
    }];
}
#define targetR 350
#define targetL -350
#define middleX [UIScreen mainScreen].bounds.size.width*0.5
- (void)panGs:(UIPanGestureRecognizer *)panGs {
    CGPoint point = [panGs translationInView:self.redView];
    if(self.redView.frame.origin.x >0){
        self.greenView.hidden = YES;
    }else{
        self.greenView.hidden = NO;
    }
    self.redView.frame = [self frameWithPointX:point.x];
    //自動(dòng)復(fù)位
    if(panGs.state == UIGestureRecognizerStateEnded){
        CGFloat target = 0;
        if(self.redView.frame.origin.x > middleX){
            target = targetR;
        }else if (CGRectGetMaxX(self.redView.frame)<middleX){
            target = targetL;
        }
        [UIView animateWithDuration:0.4 animations:^{
            CGFloat offset = target - self.redView.frame.origin.x;
            self.redView.frame = [self frameWithPointX:offset];
        }];
    }

    [panGs setTranslation:CGPointZero inView:self.redView];
}
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenFrame [UIScreen mainScreen].bounds

- (CGRect)frameWithPointX:(CGFloat)offsetX {
    CGRect frame = self.redView.frame;
    frame.origin.x += offsetX;
    CGFloat translationY = fabs(frame.origin.x* 100 / screenW);
        frame.origin.y =translationY;
    frame.size.height = [UIScreen mainScreen].bounds.size.height - 2*frame.origin.y;
    return frame;
}

- (void)setViewUp{
    //redView;
    UIView *redView = [[UIView alloc] initWithFrame:screenFrame];
    redView.backgroundColor = [UIColor redColor];
    self.redView = redView;
    //blueView
    UIView *blueView = [[UIView alloc] initWithFrame:screenFrame];
    blueView.backgroundColor = [UIColor blueColor];
    self.blueView = blueView;
    //greenView
    UIView *greenView = [[UIView alloc] initWithFrame:screenFrame];
    greenView.backgroundColor = [UIColor greenColor];
    self.greenView = greenView;
    //添加view
    [self.view addSubview:blueView];
    [self.view addSubview:greenView];
    [self.view addSubview:redView];
}

效果圖


抽屜.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過(guò)CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明AI閱讀 16,170評(píng)論 3 119
  • 對(duì)於剛剛接觸毛筆的同學(xué),如何掌握到其中的奧妙,是個(gè)重要的問(wèn)題。順為勾,逆為勒,提按之間,慢慢去感受筆豪在紙上的優(yōu)游...
  • 蘇美 原文 就說(shuō)讀書(shū),說(shuō):“金風(fēng)細(xì)細(xì),葉葉梧桐墜,綠酒初嘗人易醉,一枕小窗濃睡”。以前讀,覺(jué)得這調(diào)調(diào)挺騷:小風(fēng),小...
    笨笨熊閱讀 470評(píng)論 0 1
  • R:自選片段六:《高效學(xué)習(xí)魔法書(shū)》 I:重述原文 WHAT: 片段講述了如何利用有效的4步驟去改善不好的習(xí)慣 WH...
    不怎么會(huì)說(shuō)話的瓦片閱讀 244評(píng)論 0 0

友情鏈接更多精彩內(nèi)容