Skip to content

Commit

Permalink
add mesh decoration sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Stadnik committed Apr 6, 2012
1 parent 0cc66aa commit 900c2e2
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 7 deletions.
14 changes: 14 additions & 0 deletions BADemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
1F114BF714DBDA180052F03E /* BASeparatedTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F114BF514DBDA180052F03E /* BASeparatedTableViewController.m */; };
1F114BF814DBDA180052F03E /* BASeparatedTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1F114BF614DBDA180052F03E /* BASeparatedTableViewController.xib */; };
1F193076147001B10090774A /* BASeparatedTableProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F193075147001B10090774A /* BASeparatedTableProvider.m */; };
1F26943F152ED738004A9AA9 /* BADecoratedMeshViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F26943D152ED738004A9AA9 /* BADecoratedMeshViewController.m */; };
1F269440152ED738004A9AA9 /* BADecoratedMeshViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1F26943E152ED738004A9AA9 /* BADecoratedMeshViewController.xib */; };
1F269443152EE4EC004A9AA9 /* mesh-decoration.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F269442152EE4EC004A9AA9 /* mesh-decoration.png */; };
1F2C5F3F1473D5EB00FBB1E5 /* BANetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F2C5F3E1473D5EB00FBB1E5 /* BANetwork.m */; };
1F3CD8A114CE16E5003872C9 /* BAScrollViewProxyDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F3CD8A014CE16E5003872C9 /* BAScrollViewProxyDelegate.m */; };
1F41D24614C97D0F0073E1FF /* BAMeshViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F41D24514C97D0F0073E1FF /* BAMeshViewCell.m */; };
Expand Down Expand Up @@ -127,6 +130,10 @@
1F114BF614DBDA180052F03E /* BASeparatedTableViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BASeparatedTableViewController.xib; sourceTree = "<group>"; };
1F193074147001B10090774A /* BASeparatedTableProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BASeparatedTableProvider.h; sourceTree = "<group>"; };
1F193075147001B10090774A /* BASeparatedTableProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BASeparatedTableProvider.m; sourceTree = "<group>"; };
1F26943C152ED737004A9AA9 /* BADecoratedMeshViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BADecoratedMeshViewController.h; sourceTree = "<group>"; };
1F26943D152ED738004A9AA9 /* BADecoratedMeshViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BADecoratedMeshViewController.m; sourceTree = "<group>"; };
1F26943E152ED738004A9AA9 /* BADecoratedMeshViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BADecoratedMeshViewController.xib; sourceTree = "<group>"; };
1F269442152EE4EC004A9AA9 /* mesh-decoration.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "mesh-decoration.png"; sourceTree = "<group>"; };
1F2C5F3D1473D5EB00FBB1E5 /* BANetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BANetwork.h; sourceTree = "<group>"; };
1F2C5F3E1473D5EB00FBB1E5 /* BANetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BANetwork.m; sourceTree = "<group>"; };
1F3CD89F14CE16E5003872C9 /* BAScrollViewProxyDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BAScrollViewProxyDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -318,6 +325,7 @@
1F4B38DD138E704D003FE58C /* BADemo-Prefix.pch */,
1F726493151C750600CC51B9 /* dot-active.png */,
1F726494151C750600CC51B9 /* dot-inactive.png */,
1F269442152EE4EC004A9AA9 /* mesh-decoration.png */,
1F75710913A239450015AA92 /* form-button.png */,
1F4B38DE138E704D003FE58C /* InfoPlist.strings */,
1F4B38E0138E704D003FE58C /* Info.plist */,
Expand Down Expand Up @@ -369,6 +377,9 @@
1F0C62A614CD7E9A005FE1BB /* BAMeshViewController.h */,
1F0C62A714CD7E9A005FE1BB /* BAMeshViewController.m */,
1F0C62A814CD7E9A005FE1BB /* BAMeshViewController.xib */,
1F26943C152ED737004A9AA9 /* BADecoratedMeshViewController.h */,
1F26943D152ED738004A9AA9 /* BADecoratedMeshViewController.m */,
1F26943E152ED738004A9AA9 /* BADecoratedMeshViewController.xib */,
1FD8DEA214D6EBBF0068D009 /* BARefreshHeaderViewController.h */,
1FD8DEA314D6EBBF0068D009 /* BARefreshHeaderViewController.m */,
1FD8DEA414D6EBBF0068D009 /* BARefreshHeaderViewController.xib */,
Expand Down Expand Up @@ -648,6 +659,8 @@
1F6841EC151C6D9200ADEC62 /* BACustomPageControlViewController.xib in Resources */,
1F726495151C750600CC51B9 /* dot-active.png in Resources */,
1F726496151C750600CC51B9 /* dot-inactive.png in Resources */,
1F269440152ED738004A9AA9 /* BADecoratedMeshViewController.xib in Resources */,
1F269443152EE4EC004A9AA9 /* mesh-decoration.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -726,6 +739,7 @@
1FE8D9EB151A044F00EA38C1 /* BAPagerViewController.m in Sources */,
1FE8D9F0151B684200EA38C1 /* BACustomPageControl.m in Sources */,
1F6841EB151C6D9200ADEC62 /* BACustomPageControlViewController.m in Sources */,
1F26943F152ED738004A9AA9 /* BADecoratedMeshViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
36 changes: 36 additions & 0 deletions BADemo/BADecoratedMeshViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2012 Dmitry Stadnik. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY DMITRY STADNIK ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY STADNIK OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Dmitry Stadnik.
*/

#import <UIKit/UIKit.h>
#import "BAMeshView.h"

@interface BADecoratedMeshViewController : UIViewController <BAMeshViewDataSource, BAMeshViewDelegate>

@property(retain) IBOutlet BAMeshView *meshView;

@end
221 changes: 221 additions & 0 deletions BADemo/BADecoratedMeshViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*
Copyright 2012 Dmitry Stadnik. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY DMITRY STADNIK ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY STADNIK OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Dmitry Stadnik.
*/

#import "BADecoratedMeshViewController.h"

@interface BADecoratedMeshTestView : UIView

@end


@implementation BADecoratedMeshTestView

- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[[UIColor blackColor] set];
[[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 0.5, 0.5)] stroke];
}

@end

@implementation BADecoratedMeshViewController {
@private
NSMutableDictionary *_meshDecorationViews; // { section index:NSNumber -> decoration view:UIView }
}

@synthesize meshView = _meshView;

- (void)dealloc {
self.meshView = nil;
[_meshDecorationViews release];
[super dealloc];
}

- (void)dumpSubviews:(UIView *)view withOffset:(NSString *)offset {
NSMutableString *s = [NSMutableString string];
[s appendString:offset];
[s appendString:NSStringFromClass([view class])];
[s appendString:@" : "];
// [s appendString:NSStringFromCGRect(view.frame)];
[s appendString:NSStringFromCGPoint(view.frame.origin)];
NSLog(@"%@", s);
for (UIView *subview in view.subviews) {
[self dumpSubviews:subview withOffset:[offset stringByAppendingString:@" "]];
}
}

- (void)dumpContentViews1 {
[self dumpSubviews:self.meshView withOffset:@""];
NSLog(@"total: %d", [self.meshView.subviews count]);
}

- (void)dumpContentViews {
[self.meshView reloadData];
[self performSelector:@selector(dumpContentViews1) withObject:nil afterDelay:2];
}

- (UIView *)makeMeshDecorationView {
UIImage *image = [UIImage imageNamed:@"mesh-decoration.png"];
UIImageView *view = [[[UIImageView alloc] initWithImage:image] autorelease];
return view;
}

- (void)updateMeshDecorationViews {
if (!_meshDecorationViews) {
_meshDecorationViews = [[NSMutableDictionary alloc] init];
}
NSIndexSet *visibleSections = [self.meshView indexesOfVisibleSections];
[[_meshDecorationViews allKeys] enumerateObjectsUsingBlock:^(id sectionNumber, NSUInteger idx, BOOL *stop) {
NSInteger section = [sectionNumber intValue];
if (![visibleSections containsIndex:section]) {
UIView *decorationView = [_meshDecorationViews objectForKey:sectionNumber];
[decorationView removeFromSuperview];
[_meshDecorationViews removeObjectForKey:sectionNumber];
}
}];
[visibleSections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL *stop) {
NSNumber *sectionNumber = [NSNumber numberWithInt:section];
UIView *decorationView = [_meshDecorationViews objectForKey:sectionNumber];
if (!decorationView) {
decorationView = [self makeMeshDecorationView];
[self.view insertSubview:decorationView aboveSubview:self.meshView];
[_meshDecorationViews setObject:decorationView forKey:sectionNumber];
}
CGRect sectionRect = [self.meshView rectForSection:section];
CGRect decorationFrame = decorationView.frame;
decorationFrame.origin.x = sectionRect.origin.x + self.meshView.contentInset.left;
decorationFrame.origin.y = sectionRect.origin.y - self.meshView.contentOffset.y;
decorationView.frame = decorationFrame;
}];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateMeshDecorationViews];
}

- (void)viewDidLoad {
[super viewDidLoad];
// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Dump"
// style:UIBarButtonItemStylePlain
// target:self
// action:@selector(dumpContentViews)] autorelease];
self.meshView = [[[BAMeshView alloc] initWithFrame:self.view.bounds] autorelease];
self.meshView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.meshView.cellSize = CGSizeMake(40, 40);
self.meshView.sectionHeaderHeight = 50;
self.meshView.sectionFooterHeight = 50;
self.meshView.backgroundColor = [UIColor grayColor];
UILabel *headerLabel = [[[UILabel alloc] init] autorelease];
headerLabel.font = [UIFont boldSystemFontOfSize:15];
headerLabel.text = @"Mesh Header";
self.meshView.meshHeaderView = headerLabel;
UILabel *footerLabel = [[[UILabel alloc] init] autorelease];
footerLabel.font = [UIFont boldSystemFontOfSize:15];
footerLabel.text = @"Mesh Footer";
self.meshView.meshFooterView = footerLabel;
self.meshView.dataSource = self;
self.meshView.delegate = self;
[self.view addSubview:self.meshView];
self.meshView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);
[self updateMeshDecorationViews];
}

- (NSInteger)numberOfSectionsInMeshView:(BAMeshView *)meshView {
return 5;
}

- (NSInteger)meshView:(BAMeshView *)meshView numberOfCellsInSection:(NSInteger)section {
return 15;
}

- (BAMeshViewCell *)meshView:(BAMeshView *)meshView cellAtIndexPath:(NSIndexPath *)indexPath {
CGRect f = CGRectMake(0, 0, 80, 80);
BADecoratedMeshTestView *tv = nil;
BAMeshViewCell *cell = [meshView dequeueReusableCellWithIdentifier:@"ACell"];
if (!cell) {
cell = [[[BAMeshViewCell alloc] initWithReuseIdentifier:@"ACell"] autorelease];
cell.frame = f;
tv = [[[BADecoratedMeshTestView alloc] initWithFrame:f] autorelease];
tv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tv.contentMode = UIViewContentModeRedraw;
[cell.contentView addSubview:tv];
} else {
tv = [cell.contentView.subviews objectAtIndex:0];
}
cell.frame = f;
[tv setNeedsDisplay];
tv.backgroundColor = indexPath.meshSection % 2 ? [UIColor yellowColor] : [UIColor orangeColor];
return cell;
}

//- (CGFloat)meshView:(BAMeshView *)meshView heightForHeaderInSection:(NSInteger)section;
//- (CGFloat)meshView:(BAMeshView *)meshView heightForFooterInSection:(NSInteger)section;

//- (CGSize)meshView:(BAMeshView *)meshView sizeForCellAtIndexPath:(NSIndexPath *)indexPath {
// return CGSizeMake(70, 20 + 15 * indexPath.meshCell);
//}

- (BAMeshRowLayout)meshView:(BAMeshView *)meshView rowsLayoutInSection:(NSInteger)section {
return BAMeshRowLayoutSpreadLeft;
}

- (BAMeshCellAlignment)meshView:(BAMeshView *)meshView alignmentForCellAtIndexPath:(NSIndexPath *)indexPath {
return BAMeshCellAlignmentCenter;
}

- (UIView *)meshView:(BAMeshView *)meshView viewForHeaderInSection:(NSInteger)section {
UILabel *view = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
view.textAlignment = UITextAlignmentRight;
view.text = [NSString stringWithFormat:@"Section Header %d", section];
view.font = [UIFont boldSystemFontOfSize:15];
view.backgroundColor = [UIColor magentaColor];
return view;
}

- (UIView *)meshView:(BAMeshView *)meshView viewForFooterInSection:(NSInteger)section {
UILabel *view = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
view.textAlignment = UITextAlignmentRight;
view.text = [NSString stringWithFormat:@"Section Footer %d", section];
view.font = [UIFont boldSystemFontOfSize:15];
view.backgroundColor = [UIColor greenColor];
return view;
}

- (NSIndexPath *)meshView:(BAMeshView *)meshView willSelectCellAtIndexPath:(NSIndexPath *)indexPath {
return indexPath;
}

- (void)meshView:(BAMeshView *)meshView didSelectCellAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%d/%d", indexPath.meshSection, indexPath.meshCell);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

@end
Loading

0 comments on commit 900c2e2

Please sign in to comment.