Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisonXi committed Nov 28, 2017
1 parent dbc61f8 commit 76f729a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
6 changes: 3 additions & 3 deletions LazyScroll.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "LazyScroll"
s.version = "1.0"
s.version = "1.0.0"
s.summary = "A ScrollView to resolve the problem of reusability of views."

s.description = <<-DESC
Expand All @@ -14,10 +14,10 @@ Pod::Spec.new do |s|
"HarrisonXi" => "[email protected]" }
s.platform = :ios
s.ios.deployment_target = "5.0"
s.source = { :git => "https://github.com/alibaba/LazyScrollView.git", :tag => "1.0" }
s.source = { :git => "https://github.com/alibaba/LazyScrollView.git", :tag => "1.0.0" }
s.source_files = "LazyScrollView/*.{h,m}"
s.requires_arc = true

# s.dependency 'TMUtils', '~> 1.0'
s.dependency 'TMUtils', '~> 1.0'

end
57 changes: 29 additions & 28 deletions LazyScrollViewDemo/LazyScrollViewDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@

@interface LazyScrollViewCustomView : UILabel <TMMuiLazyScrollViewCellProtocol>

@property (nonatomic, assign) NSUInteger reuseTimes;

@end

@implementation LazyScrollViewCustomView

- (void)mui_prepareForReuse
{
NSLog(@"%@ - Prepare For Reuse", self.text);
}

- (void)mui_didEnterWithTimes:(NSUInteger)times
{
NSLog(@"%@ - Did Enter With Times - %zd", self.text, times);
}

- (void)mui_afterGetView
{
NSLog(@"%@ - AfterGetView", self.text);
self.reuseTimes++;
}

@end
Expand All @@ -48,27 +40,31 @@ - (void)viewDidLoad {
TMMuiLazyScrollView *scrollview = [[TMMuiLazyScrollView alloc] init];
scrollview.frame = self.view.bounds;
scrollview.dataSource = self;
scrollview.autoAddSubview = YES;
[self.view addSubview:scrollview];

// Here is frame array for test.
// LazyScrollView must know every rect before rending.
rectArray = [[NSMutableArray alloc] init];

CGFloat maxY = 0, currentY = 10;
CGFloat viewWidth = CGRectGetWidth(self.view.bounds);
// Create a single column layout with 5 elements;
for (int i = 0; i < 5; i++) {
[rectArray addObject:[NSValue valueWithCGRect:CGRectMake(10, i * 80 + 2 , self.view.bounds.size.width - 20, 80 - 2)]];
[self addRect:CGRectMake(10, i * 80 + currentY, viewWidth - 20, 80 - 3) andUpdateMaxY:&maxY];
}
// Create a double column layout with 10 elements;
currentY = maxY + 10;
for (int i = 0; i < 10; i++) {
[rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i % 2) * self.view.bounds.size.width / 2 + 3, 410 + i / 2 * 80 + 2 , self.view.bounds.size.width / 2 - 3, 80 - 2)]];
[self addRect:CGRectMake((i % 2) * (viewWidth - 20 + 3) / 2 + 10, i / 2 * 80 + currentY, (viewWidth - 20 - 3) / 2, 80 - 3) andUpdateMaxY:&maxY];
}
// Create a trible column layout with 15 elements;
currentY = maxY + 10;
for (int i = 0; i < 15; i++) {
[rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i % 3) * self.view.bounds.size.width / 3 + 1, 820 + i / 3 * 80 + 2 , self.view.bounds.size.width / 3 - 3, 80 - 2)]];
[self addRect:CGRectMake((i % 3) * (viewWidth - 20 + 6) / 3 + 10, i / 3 * 80 + currentY, (viewWidth - 20 - 6) / 3, 80 - 3) andUpdateMaxY:&maxY];
}
scrollview.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 1230);

// STEP 3 reload LazyScrollView
scrollview.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds), maxY + 10);
[scrollview reloadData];
}

Expand All @@ -93,21 +89,32 @@ - (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *
LazyScrollViewCustomView *label = (LazyScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"testView"];
NSInteger index = [muiID integerValue];
if (!label) {
label = [[LazyScrollViewCustomView alloc]initWithFrame:[(NSValue *)[rectArray objectAtIndex:index] CGRectValue]];
NSLog(@"create a new label");
label = [LazyScrollViewCustomView new];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
label.reuseIdentifier = @"testView";
label.backgroundColor = [self randomColor];
}
label.frame = [(NSValue *)[rectArray objectAtIndex:index] CGRectValue];
label.text = [NSString stringWithFormat:@"%zd", index];
label.backgroundColor = [self randomColor];
[scrollView addSubview:label];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)]];
if (label.reuseTimes > 0) {
label.text = [NSString stringWithFormat:@"%zd\nlast index: %@\nreuse times: %zd", index, label.muiID, label.reuseTimes];
} else {
label.text = [NSString stringWithFormat:@"%zd", index];
}
return label;
}

#pragma mark - Private

- (void)addRect:(CGRect)newRect andUpdateMaxY:(CGFloat *)maxY
{
if (CGRectGetMaxY(newRect) > *maxY) {
*maxY = CGRectGetMaxY(newRect);
}
[rectArray addObject:[NSValue valueWithCGRect:newRect]];
}

- (UIColor *)randomColor
{
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
Expand All @@ -116,10 +123,4 @@ - (UIColor *)randomColor
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (void)click:(UIGestureRecognizer *)recognizer
{
LazyScrollViewCustomView *label = (LazyScrollViewCustomView *)recognizer.view;
NSLog(@"Click - %@", label.muiID);
}

@end
2 changes: 1 addition & 1 deletion LazyScrollViewDemo/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ platform :ios, '8.0'

target 'LazyScrollViewDemo' do
pod 'LazyScroll', :path => '../'
pod 'TMUtils', :path => '../'
# pod 'TMUtils', :path => '../'
end
4 changes: 2 additions & 2 deletions TMUtils.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|

s.name = "TMUtils"
s.version = "1.0"
s.version = "1.0.0"
s.summary = "Common safe methods & utils for NSArray & NSDictionary."
s.homepage = "https://github.com/alibaba/LazyScrollView"
s.license = {:type => 'MIT'}
s.author = { "HarrisonXi" => "[email protected]" }
s.platform = :ios
s.ios.deployment_target = "5.0"
s.source = { :git => "https://github.com/alibaba/LazyScrollView.git", :tag => "1.0" }
s.source = { :git => "https://github.com/alibaba/LazyScrollView.git", :tag => "1.0.0" }
s.source_files = "TMUtils/*.{h,m}"
s.requires_arc = true

Expand Down

0 comments on commit 76f729a

Please sign in to comment.