iOS 常用特效篇【長(zhǎng)期更新】-最近更新:2017-01-13

特效!特效!特效!重要的事情說(shuō)三遍

1.tabbar 點(diǎn)擊按鈕 特效

@interface TabBar : UITabBar

方法中 添加上這個(gè)方法 就可以做出 點(diǎn)擊 tabbar 圖標(biāo)彈起來(lái)收回去再?gòu)椘饋?lái)的效果 至于效果圖 不好意思 gif不會(huì)做丑勤。错维。奖地。。赋焕。

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    for (UIControl *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}


- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要實(shí)現(xiàn)的幀動(dòng)畫(huà)
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 1;
            animation.calculationMode = kCAAnimationCubic;
            //把動(dòng)畫(huà)添加上去
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}

2 tableview 下拉背景圖拉伸不現(xiàn)實(shí)空白

截圖

WeChat_1484284543.jpeg

好 下面來(lái)說(shuō)一下思路

首先 這是一個(gè)可以滑動(dòng)的tableview

那么 其實(shí)這是一個(gè)視覺(jué)欺騙

星空背景 跟 頭像名字 其實(shí)不是一個(gè)東西~

頭像跟名字是tableview的第0個(gè)cell
而星空背景 是貼在tableview上面的一張圖片

下面我們就需要 讓tableview 繼承代理

//HeadViewH 這是圖片的高度
//Gato_Width 屏幕寬
//underImage 背景圖片(星空)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsety = scrollView.contentOffset.y+scrollView.contentInset.top;
    if (offsety <= 0)
    {

        //放大
        CGRect frame= underImage.frame;
        frame.size.height = HeadViewH - offsety;
        frame.origin.x = offsety / 2;
        frame.size.width = Gato_Width - offsety;
        
        underImage.frame = frame;
        
    }
    else
    {
    }

}

然后你就可以隨意拉了参歹。。隆判。當(dāng)然 你需要一張高清的圖片當(dāng)作背景 然后被拉伸以后 他會(huì)變得很丑犬庇!

當(dāng)然 你可能會(huì)想要另一種效果 就是 起初UINavigationBar 看不到 上滑頁(yè)面以后 UINavigationBar慢慢顯示出來(lái)

網(wǎng)上也有很多demo 其實(shí)很簡(jiǎn)單

只需要在 scrollviewdidscroll方法中 根據(jù)高度進(jìn)行隱藏就好

貼上代碼

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //1。拿到y(tǒng)值
    CGFloat contentOffY = scrollView.contentOffset.y;
    
    if (contentOffY > - 20) {
        self.navigationController.navigationBar.alpha = 20 / contentOffY;
        if (20 / contentOffY < 0.1) {
            self.navigationController.navigationBar.alpha = 0;
        }
    }else{
        self.navigationController.navigationBar.alpha = 1;
    }
}

3 關(guān)于下拉 頭像跟背景改變侨嘀,這是兩種狀態(tài)

效果圖????


下拉頭像改變效果圖

思路
利用了 scrollView 的滑動(dòng)監(jiān)聽(tīng) 動(dòng)態(tài)改變 image view 屬性

P1.NavgationBar 代碼

#import "NavgationBarViewController.h"
#define MaxIconWH  70.0  //用戶(hù)頭像最大的尺寸
#define MinIconWH  30.0  // 用戶(hù)頭像最小的頭像
#define MaxIconCenterY  44 // 頭像最大的centerY
#define MinIconCenterY 22
#define maxScrollOff 180
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface NavgationBarViewController ()<UITableViewDelegate>

/**
 *  用戶(hù)頭像
 */
@property (strong, nonatomic) UIImageView * titleIMg;

/**
 *  tableview
 */
@property (weak, nonatomic) IBOutlet UIScrollView * tableVIew;

@end

@implementation NavgationBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
   // self.tableVIew.bounces = NO;
    self.tableVIew.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+ 50);
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:self.titleIMg];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.titleIMg removeFromSuperview];
}





- (UIImageView *)titleIMg{
    if(_titleIMg == nil){
        UIImageView * img = [[UIImageView alloc] init];
        img.image = [UIImage imageNamed:@"1.jpg"];
        img.bounds = CGRectMake(0, 0, MaxIconWH, MaxIconWH);
        img.center = CGPointMake(SCREEN_WIDTH*0.5, MaxIconCenterY);
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.borderColor = [UIColor whiteColor].CGColor;
        img.layer.borderWidth = 1.2;
        img.layer.cornerRadius = MaxIconWH/2.0;
        img.layer.masksToBounds = YES;
        _titleIMg = img;
    }
    return _titleIMg;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    // 是scrollview的偏移量
    CGFloat updateY = scrollView.contentOffset.y ;
    NSLog(@"%f",scrollView.contentOffset.y);
    
    
    //  隨著scrollview 的滾動(dòng)械筛, 改變bounds
    CGRect bound = _titleIMg.bounds;
    // 隨著滾動(dòng)縮減的頭像的尺寸
    CGFloat reduceW =  updateY  *(MaxIconWH - MinIconWH)/(maxScrollOff - 64);
    reduceW = reduceW < 0 ? 0 : reduceW;
    // 寬度縮小的幅度要和headview偏移的幅度成比例,但是最小的寬度不能小于MinIconWH
    CGFloat yuanw =  MAX(MinIconWH, MaxIconWH - reduceW);
    _titleIMg.layer.cornerRadius = yuanw/2.0;
    bound.size.height = yuanw;
    bound.size.width  = yuanw;
    _titleIMg.bounds = bound;
    
    
    // 改變center  max - min 是滾動(dòng) center y值 最大的偏差
    CGPoint center = _titleIMg.center;
    CGFloat yuany =  MAX(MinIconCenterY, MaxIconCenterY - updateY * (MaxIconCenterY - MinIconCenterY)/(maxScrollOff - 64) ) ;
    yuany = yuany > MaxIconCenterY ? MaxIconCenterY : yuany;
    
    center.y = yuany;
    _titleIMg.center = center;
    

    
}

@end

P2.自定義View

#define HeadHeight 278 // headView的高度
#define MaxIconWH  100.0  //用戶(hù)頭像最大的尺寸
#define MinIconWH  30.0  // 用戶(hù)頭像最小的頭像
#define MaxIconCenterY 120 // 頭像最大的centerY
#define MinIconCenterY 42
#define HeadContentOffset @"contentOffset"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define DRHColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *backScroll;
/**
 *  表頭的view
 */
@property (strong, nonatomic) UIView * headView;
/**
 *  代替navigationbar 的自定義view
 */
@property (weak, nonatomic) IBOutlet UIView *navbarView;

/**
 *  用戶(hù)頭像
 */
@property (strong, nonatomic) UIImageView * titleIMg;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpScrollViewHeadView];
    [self.backScroll addSubview:self.headView];
    [self.navbarView addSubview:self.titleIMg];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}


/**
 *  初始化headview
 */
- (void)setUpScrollViewHeadView{
    
    self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 2);
    self.backScroll.contentInset = UIEdgeInsetsMake(HeadHeight, 0, 0, 0);
    // KVO
    [self.backScroll addObserver:self forKeyPath:HeadContentOffset  options:NSKeyValueObservingOptionNew context:nil];
    [self.backScroll setContentOffset: CGPointMake(0, -HeadHeight) animated:YES];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if(![keyPath isEqualToString:HeadContentOffset])
        return;
    [self scrollViewDidScroll:self.backScroll];
}


- (UIView *)headView{
    if(_headView == nil){
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, -HeadHeight, SCREEN_WIDTH, HeadHeight)];
        _headView.backgroundColor = DRHColor(233, 73, 71);
    }
    return _headView;
}


- (UIImageView *)titleIMg{
    if(_titleIMg == nil){
        UIImageView * img = [[UIImageView alloc] init];
        img.center = CGPointMake(SCREEN_WIDTH/2.0, MaxIconCenterY);
        img.bounds = CGRectMake(0, 0, MaxIconWH ,MaxIconWH );
         img.image = [UIImage imageNamed:@"1.jpg"];
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.borderColor = [UIColor whiteColor].CGColor;
        img.layer.borderWidth = 1.2;
        img.layer.cornerRadius = MaxIconWH/2.0;
        img.layer.masksToBounds = YES;
        _titleIMg = img;
    }
    return _titleIMg;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    if(offsetY < -HeadHeight) {
        CGRect currentFrame = self.headView.frame;
        currentFrame.origin.y = offsetY;
        currentFrame.size.height = -1*offsetY;
        self.headView.frame = currentFrame;
    }
    
    // 是scrollview的偏移量
    CGFloat updateY = scrollView.contentOffset.y + HeadHeight;
    //NSLog(@"%f_____________%f",scrollView.contentOffset.y, updateY);

    
    //  隨著scrollview 的滾動(dòng)飒炎, 改變bounds
      CGRect bound = _titleIMg.bounds;
    // 隨著滾動(dòng)縮減的頭像的尺寸
     CGFloat reduceW = updateY  *(MaxIconWH - MinIconWH)/(HeadHeight - 64);
    // 寬度縮小的幅度要和headview偏移的幅度成比例,但是最小的寬度不能小于MinIconWH
     CGFloat yuanw =  MAX(MinIconWH, MaxIconWH - reduceW);
    
    NSLog(@"-----%f+++++++%f",reduceW,yuanw);
    
    
    _titleIMg.layer.cornerRadius = yuanw/2.0;
    bound.size.height = yuanw;
    bound.size.width  = yuanw;
    _titleIMg.bounds = bound;

    
    // 改變center  max - min 是滾動(dòng) center y值 最大的偏差
    CGPoint center = _titleIMg.center;
    CGFloat yuany =  MAX(MinIconCenterY, MaxIconCenterY - updateY * (MaxIconCenterY - MinIconCenterY)/(HeadHeight - 64) ) ;
    
    center.y = yuany;
    _titleIMg.center = center;

}
@end

注意:本段代碼 來(lái)自于CocoaChina 作者 [風(fēng)飄飄的油菜花] 如果作者感覺(jué)侵權(quán)請(qǐng)聯(lián)系我笆豁,我會(huì)及時(shí)刪除侵權(quán)信息


4.點(diǎn)擊按鈕 彈出陰影選擇框

效果圖:


例子

demo :傳送門(mén)--點(diǎn)擊我跳轉(zhuǎn)下載鏈接

關(guān)鍵代碼:

- (IBAction)btnTitleTapAction:(UIButton *)btn
{
    YJPopoverContentController *contentController = [[YJPopoverContentController alloc] init];
    
    YJPopoverController *popController = [[YJPopoverController alloc] initWithContentViewController:contentController popoverContentSize:CGSizeMake(150, 35 * 3 + 10)];
    [popController presentPopoverFromRect:btn.frame inView:btn.superview permitterArrowDirections:YJPopoverArrowDirection_up animated:YES];
}

- (IBAction)btnPopDownTapAction:(UIButton *)btn
{
    YJPopoverContentController *contentController= [[YJPopoverContentController alloc] init];
    
    YJPopoverController *popController = [[YJPopoverController alloc] initWithContentViewController:contentController popoverContentSize:CGSizeMake(150, 35 * 3 + 10)];
    [popController presentPopoverFromRect:btn.frame inView:btn.superview permitterArrowDirections:YJPopoverArrowDirection_down animated:YES];
}

- (IBAction)btnPopLeftTapAction:(UIButton *)btn
{
    YJPopoverContentController *contentController = [[YJPopoverContentController alloc] init];
    
    YJPopoverController *popController = [[YJPopoverController alloc] initWithContentViewController:contentController popoverContentSize:CGSizeMake(150, 35 * 3 + 10)];
    [popController presentPopoverFromRect:btn.frame inView:btn.superview permitterArrowDirections:YJPopoverArrowDirection_left animated:YES];
}

- (IBAction)btnPopRightTapAction:(UIButton *)btn
{
    YJPopoverContentController *contentController = [[YJPopoverContentController alloc] init];
    
    YJPopoverController *popController = [[YJPopoverController alloc] initWithContentViewController:contentController popoverContentSize:CGSizeMake(150, 35 * 3 + 10)];
    [popController presentPopoverFromRect:btn.frame inView:btn.superview permitterArrowDirections:YJPopoverArrowDirection_right animated:YES];
}

簡(jiǎn)單說(shuō)一下這個(gè)demo跟三方的思路

點(diǎn)開(kāi)的陰影部分 是一個(gè)新的controller 里面有一個(gè)tableview「這樣就方便更改點(diǎn)開(kāi)陰影部分樣式了」

當(dāng)然 大小也都是可以控制的

然后就是利用

YJPopoverController *popController = [[YJPopoverController alloc] initWithContentViewController:contentController popoverContentSize:CGSizeMake(150, 35 * 3 + 10)];

[popController presentPopoverFromRect:btn.frame inView:btn.superview permitterArrowDirections:YJPopoverArrowDirection_up animated:YES];

這行代碼搞定
其中呢

YJPopoverController  == 三方Controller
contentController == 彈出樣式controller 【可以是任何東西 只要你想得到】
CGSizeMake = 彈出controller的坐標(biāo)
typedef NS_ENUM(NSUInteger, YJPopoverArrowDirection) {
    YJPopoverArrowDirection_up,         //箭頭向上
    YJPopoverArrowDirection_left,       //箭頭向左
    YJPopoverArrowDirection_down,       //箭頭向下
    YJPopoverArrowDirection_right       //箭頭向右
};

好 這個(gè)特效結(jié)束郎汪。


5 綜合案例動(dòng)畫(huà)

4.綜合案例

綜合案例-path


綜合案例-path.gif

綜合案例-釘釘按鈕


綜合案例-釘釘按鈕.gif

綜合案例-點(diǎn)贊


綜合案例-點(diǎn)贊.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市闯狱,隨后出現(xiàn)的幾起案子煞赢,更是在濱河造成了極大的恐慌,老刑警劉巖哄孤,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件照筑,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡瘦陈,警方通過(guò)查閱死者的電腦和手機(jī)凝危,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)晨逝,“玉大人蛾默,你說(shuō)我怎么就攤上這事∽矫玻” “怎么了支鸡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)趁窃。 經(jīng)常有香客問(wèn)我牧挣,道長(zhǎng),這世上最難降的妖魔是什么醒陆? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任瀑构,我火速辦了婚禮,結(jié)果婚禮上统求,老公的妹妹穿的比我還像新娘检碗。我一直安慰自己据块,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布折剃。 她就那樣靜靜地躺著另假,像睡著了一般。 火紅的嫁衣襯著肌膚如雪怕犁。 梳的紋絲不亂的頭發(fā)上边篮,一...
    開(kāi)封第一講書(shū)人閱讀 52,682評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音奏甫,去河邊找鬼戈轿。 笑死,一個(gè)胖子當(dāng)著我的面吹牛阵子,可吹牛的內(nèi)容都是我干的思杯。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼挠进,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼色乾!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起领突,我...
    開(kāi)封第一講書(shū)人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤暖璧,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后君旦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體澎办,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年金砍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了局蚀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡恕稠,死狀恐怖至会,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情谱俭,我是刑警寧澤奉件,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站昆著,受9級(jí)特大地震影響县貌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜凑懂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一煤痕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦摆碉、人聲如沸塘匣。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)忌卤。三九已至,卻和暖如春楞泼,著一層夾襖步出監(jiān)牢的瞬間驰徊,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工堕阔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棍厂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓超陆,卻偏偏與公主長(zhǎng)得像牺弹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子时呀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容