学习iOS,今天试着做一个简单的引导页,由于的是新手,写的不好,不要见怪
首先创建一个引导页文件,这里我命名为GuideView
GuideView.h文件
#import <UIKit/UIKit.h>
//自定义代理,实现按钮方法
@protocol GuideDelegate <NSObject>
-(void)enter;
@end
@interface GuideView : UIScrollView<UIScrollViewDelegate>
@property (nonatomic,weak) id<GuideDelegate>guidedelegate;
@end
GuideView.m文件
#import "GuideView.h"
#define PAGE_COUNT 2
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HIEGHT ([UIScreen mainScreen].bounds.size.height)
@interface GuideView(){
UIPageControl *page;
}
@end
@implementation GuideView
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.bounces = NO;
self.contentSize = CGSizeMake(SCREEN_WIDTH*PAGE_COUNT, SCREEN_HIEGHT);
self.backgroundColor = [UIColor whiteColor];
self.showsHorizontalScrollIndicator = NO;
self.pagingEnabled = YES;
self.delegate = self;
self.backgroundColor = [UIColor clearColor];
for (int i = 0; i < PAGE_COUNT; i++) {
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HIEGHT)];
[view setBackgroundColor:[UIColor whiteColor]];
[view setImage:[UIImage imageNamed:[NSString stringWithFormat:@"guide%d",i+1]]];
[self addSubview:view];
if (i == 1) {
UIButton *enter = [UIButton buttonWithType:UIButtonTypeSystem];
enter.frame = CGRectMake(SCREEN_WIDTH / 2 - 50, SCREEN_HIEGHT-100, 100, 50);
[enter setTitle:@"Enter" forState:UIControlStateNormal];
[enter addTarget:self action:@selector(enterBtn) forControlEvents:UIControlEventTouchUpInside];
view.userInteractionEnabled = YES;
[view addSubview:enter];
}
}
}
return self;
}
//这个没有用,晓得问题的麻烦告诉一下
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x > SCREEN_WIDTH * 2 + 20) {
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
-(void)enterBtn{
[self.guidedelegate enter];
}
@end
使用引导页
#import "GuidePageView.h"
#import "GuideView.h"
#pragma mark 获取屏幕尺寸
#define SCREEN_FRAME ([UIScreen mainScreen].bounds)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HRIGHT ([UIScreen mainScreen].bounds.size.height)
@interface GuidePageView()<GuideDelegate>
@end
@implementation GuidePageView
-(void)viewDidLoad{
[super viewDidLoad];
GuideView *g = [[GuideView alloc] initWithFrame:SCREEN_FRAME];
g.guidedelegate = self;
[self.view addSubview:g];
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
-(void)enter{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
效果
后续会继续完善