Skip to content

Commit

Permalink
Add project.
Browse files Browse the repository at this point in the history
  • Loading branch information
@wxin9 committed Jul 3, 2012
1 parent 5934411 commit dac5986
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 0 deletions.
3 changes: 3 additions & 0 deletions FantasyView/AppDelegate.h
Expand Up @@ -8,8 +8,11 @@

#import <UIKit/UIKit.h>

@class RootViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) RootViewController *controller;

@end
46 changes: 46 additions & 0 deletions FantasyView/FantasyView.h
@@ -0,0 +1,46 @@
//
// FantasyView.h
// FantasyView
//
// Created by 昕 王 on 12-7-2.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>



@protocol FantasyViewDataSource;
@protocol FantasyViewDelegate;

@interface FantasyView : UIView

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) id<FantasyViewDataSource> dataSource;
@property (nonatomic, assign) id<FantasyViewDelegate> delegate;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) BOOL pagingEnabled;

- (void)reloadData;
- (void)fantasyViewScrollToIndex:(NSInteger)index animation:(BOOL)animation;
- (UIView *)getViewInFantasyViewWithIndex:(NSInteger)index;

@end

@protocol FantasyViewDelegate <NSObject>

@optional
- (void)fantasyView:(FantasyView *)fanView selectIndex:(NSInteger)index;
- (void)fantasyView:(FantasyView *)fanView scrollToIndex:(NSInteger)index;

@end

@protocol FantasyViewDataSource <NSObject>

@required
- (CGFloat)fantasyView:(FantasyView *)fanView widthForIndex:(NSInteger)index;
- (NSInteger)numberOfIndexForFantasyView:(FantasyView *)fanView;
- (void)fantasyView:(FantasyView *)fanView setContentView:(UIView *)contentView ForIndex:(NSInteger)index;
- (UIView *)fantasyView:(FantasyView *)fanView targetRect:(CGRect)targetRect ForIndex:(NSInteger)index;

@end
168 changes: 168 additions & 0 deletions FantasyView/FantasyView.m
@@ -0,0 +1,168 @@
//
// FantasyView.m
// FantasyView
//
// Created by 昕 王 on 12-7-2.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FantasyView.h"
#import <QuartzCore/QuartzCore.h>

#define kTableView_Tag 2199
#define kTableView_ContentView_Tag 1231

@interface FantasyView () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate> {
CGSize _size;
}

- (void)createTableView;

@end

@implementation FantasyView
@synthesize tableView = _tableView;
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize currentIndex = _currentIndex;
@synthesize pagingEnabled = _pagingEnabled;

- (void)dealloc{
self.dataSource = nil;
self.tableView = nil;
self.delegate = nil;
[super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_size = frame.size;
self.currentIndex = -1;
[self createTableView];
}
return self;
}

#pragma mark - Private Method
- (void)createTableView{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)];
tableView.tag = kTableView_Tag;
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundColor = [UIColor clearColor];
tableView.transform = CGAffineTransformMakeRotation(-M_PI/2);
self.tableView.frame = CGRectMake(0, 0, _size.width, _size.height);
tableView.showsVerticalScrollIndicator = NO;
tableView.showsVerticalScrollIndicator = NO;
[self addSubview:tableView];
[tableView release];

}

#pragma mark - Public Method

- (void)reloadData{
_size = self.frame.size;
self.tableView.frame = CGRectMake(0, 0, _size.width, _size.height);

[self.tableView reloadData];
}

- (void)setPagingEnabled:(BOOL)pagingEnabled{
if (_pagingEnabled != pagingEnabled) {
_pagingEnabled = pagingEnabled;
self.tableView.pagingEnabled = _pagingEnabled;
}
}

- (UIView *)getViewInFantasyViewWithIndex:(NSInteger)index{
return nil;
}

- (UITableView *)tableView{
return (UITableView *)[self viewWithTag:kTableView_Tag];
}

- (void)fantasyViewScrollToIndex:(NSInteger)index animation:(BOOL)animation{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:animation];
}

#pragma mark - UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.dataSource respondsToSelector:@selector(fantasyView:widthForIndex:)]) {
return [self.dataSource fantasyView:self widthForIndex:indexPath.row];
}
else {
return 0.f;
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self.dataSource respondsToSelector:@selector(numberOfIndexForFantasyView:)]) {
return [self.dataSource numberOfIndexForFantasyView:self];
}
else {
return 0;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"fantasyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.frame = CGRectMake(0, 0, [self.dataSource fantasyView:self widthForIndex:indexPath.row], _size.height);
cell.contentView.frame = cell.bounds;
cell.selectionStyle = UITableViewCellSelectionStyleNone;

UIView *contentView = [self.dataSource fantasyView:self targetRect:cell.contentView.frame ForIndex:indexPath.row];
if (!contentView) {
contentView = [[[UIView alloc] initWithFrame:cell.contentView.bounds] autorelease];

}
contentView.transform = CGAffineTransformMakeRotation(M_PI/2);
contentView.frame = CGRectMake(0, 0, cell.contentView.frame.size.height, cell.contentView.frame.size.width);
contentView.tag = kTableView_ContentView_Tag;
[cell.contentView addSubview:contentView];

}

cell.frame = CGRectMake(0, 0, [self.dataSource fantasyView:self widthForIndex:indexPath.row], _size.height);
cell.contentView.frame = cell.bounds;
UIView *contentView = [cell.contentView viewWithTag:kTableView_ContentView_Tag];
contentView.frame = CGRectMake(0, 0, cell.contentView.frame.size.height, cell.contentView.frame.size.width);
[self.dataSource fantasyView:self setContentView:contentView ForIndex:indexPath.row];
return cell;

}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.delegate respondsToSelector:@selector(fantasyView:selectIndex:)]) {
[self.delegate fantasyView:self selectIndex:indexPath.row];
}
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.pagingEnabled) {
CGFloat pageWidth = scrollView.frame.size.width;
int currentPage = floor((scrollView.contentOffset.y - pageWidth/2)/pageWidth)+1;
if (self.currentIndex != currentPage) {
if ([self.delegate respondsToSelector:@selector(fantasyView:scrollToIndex:)]) {
[self.delegate fantasyView:self scrollToIndex:currentPage];
}
self.currentIndex = currentPage;
}
}
}



@end
13 changes: 13 additions & 0 deletions FantasyView/RootViewController.h
@@ -0,0 +1,13 @@
//
// RootViewController.h
// FantasyView
//
// Created by 昕 王 on 12-7-2.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
143 changes: 143 additions & 0 deletions FantasyView/RootViewController.m
@@ -0,0 +1,143 @@
//
// RootViewController.m
// FantasyView
//
// Created by 昕 王 on 12-7-2.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "FantasyView.h"
#import <QuartzCore/QuartzCore.h>


@interface RootViewController () <FantasyViewDataSource, FantasyViewDelegate> {
UIInterfaceOrientation _orientation;
}
@property (nonatomic, retain) FantasyView *fantasyView;
@property (nonatomic, retain) UIImageView *imagView;
@end

@implementation RootViewController
@synthesize fantasyView = _fantasyView;
@synthesize imagView = _imagView;

- (void)dealloc{
self.fantasyView = nil;
self.imagView = nil;
[super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)loadView{
[super loadView];
FantasyView *fantasyView = [[FantasyView alloc] initWithFrame:self.view.bounds];
fantasyView.dataSource = self;
fantasyView.delegate = self;
fantasyView.backgroundColor = [UIColor clearColor];
fantasyView.pagingEnabled = YES;
self.fantasyView = fantasyView;
[fantasyView release];
[self.view addSubview:self.fantasyView];

_orientation = [UIApplication sharedApplication].statusBarOrientation;
[self.fantasyView reloadData];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(30, 30, 100, 100);
[btn setTitle:@"GO" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}

- (void)action{
[self.fantasyView fantasyViewScrollToIndex:12 animation:NO];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
_orientation = toInterfaceOrientation;
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
self.fantasyView.frame = CGRectMake(0, 0, 768, 1024);
}
else {
self.fantasyView.frame = CGRectMake(0, 0, 1024, 768);
}
[self.fantasyView reloadData];
[self.fantasyView fantasyViewScrollToIndex:self.fantasyView.currentIndex animation:NO];

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
}

- (CGFloat)fantasyView:(FantasyView *)fanView widthForIndex:(NSInteger)index{
if (UIInterfaceOrientationIsPortrait(_orientation)) {
return 768.f;
}
else {
return 1024.f;
}
}

- (NSInteger)numberOfIndexForFantasyView:(FantasyView *)fanView{
return 100;
}

- (void)fantasyView:(FantasyView *)fanView setContentView:(UIView *)contentView ForIndex:(NSInteger)index{
if ([contentView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)contentView;
if (index % 2 == 0) {
imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"jpeg"]];

}
else {
imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cba" ofType:@"jpeg"]];

}
UILabel *label = (UILabel *)[imageView viewWithTag:123];
label.text =[NSString stringWithFormat:@"%d", index];
}
}

- (UIView *)fantasyView:(FantasyView *)fanView targetRect:(CGRect)targetRect ForIndex:(NSInteger)index{
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:targetRect] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:imageView.bounds] autorelease];
label.font = [UIFont systemFontOfSize:50.f];
label.tag = 123;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[imageView addSubview:label];
return imageView;
}

- (void)fantasyView:(FantasyView *)fanView scrollToIndex:(NSInteger)index{
NSLog(@"%d", index);
}


@end
Binary file added FantasyView/cba.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dac5986

Please sign in to comment.