需求
实现简单的APP界面
功能:
- 实现滚动
- 实现上层、下层横栏滚动时穿透效果(永远浮在表面,不跟着滚动)。暂用UIView代替,还没学Bar。
分析:
知识点:
- 实现鼠标拖动的上下滚动:当把各个带背景图的子控件添加到UIScrollView中时,就能。
- 上下导航栏没有参与滚动:它们并不属于UIScrollView的子控件。
本质是和UIScrollView同层的视图,且有不透明度。
其中的文字,通过UILabel设置,其它API的原理也一样,都是要通过添加内容到视图上的方式来设置。
- 需要掌握USIScrollView三个属性的应用:
1> 可滚动:设置contentSize。若宽度为屏幕宽度则横向不能滚动,如果高度大于屏幕,则实现滚动。
2> 初始显示偏移:如下,要让橙色别盖住下面,但是顶层topBar又不是scrollView的子视图。
要设置偏移量。需要偏移UIView大小加上margintop大小。
设置contentInsetOffset:CGPointMake(0, -100)。注意偏移量是负数
3> 但是拖动后,又会恢复原来被覆盖的样子,希望永远不回去,还需要设置内边距,所以是第三个属性:contentInset,参数为(上,左,下,右)。
实现
自定义类:
声明需要的全部组件
关于weak和strong的简单选用:当前类是否拥有它。
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIScrollViewHimalayanTest : UIView
// 1. 声明组件:几个按钮:(因为可点击)
/*
weak或strong选取:当前类拥有它,则声明为strong,否则为weak
*/
@property(nonatomic, strong) UIScrollView* scrollveiw;
@property(nonatomic, strong) UIView* topbar;
@property(nonatomic, strong) UIView* bottombar;
@property(nonatomic, strong) UIButton* l1;
@property(nonatomic, strong) UIButton* l2;
@property(nonatomic, strong) UIButton* l3;
@property(nonatomic, strong) UIButton* r1;
@property(nonatomic, strong) UIButton* r2;
@property(nonatomic, strong) UIButton* r3;
@property(nonatomic, strong) UIButton* bottom1;
@end
实现:
仅仅是scrollView加其它UI的创建和初始化
//
// UIScrollViewHimalayanTest.m
// OCUI_Learn
//
// Created by AAA on 2024/6/3.
//
#import "UIScrollViewHimalayanTest.h"
@implementation UIScrollViewHimalayanTest
-(instancetype) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
/*
创建滚动组件、按钮:
w、h、margin
contentSize:必须给大小,如果内容很小,必然不能滚动,滚动的本质是UIScrollView的contentSize很大。
宽度设置为屏幕宽度即可,这样横向就不能滚动
*/
CGFloat w = (self.frame.size.width - 30) /2;
CGFloat h = w;
CGFloat marginleft = 10;
CGFloat margintop