[iOS_Dev] UITableView 水平放置 自动循环滚动

来源:互联网 发布:java spring 日志记录 编辑:程序博客网 时间:2024/05/28 15:06
//table  原创 http://blog.csdn.net/u014558625/- (id)initWithFrame:(CGRect)frameOfHorizontalTwoTableView style:(UITableViewStyle)style{    float x = frameOfHorizontalTwoTableView.origin.x;    float y = frameOfHorizontalTwoTableView.origin.y;    float w = frameOfHorizontalTwoTableView.size.width;    float h = frameOfHorizontalTwoTableView.size.height;    float xOrigin = x + (w - h)/2;    float yOrigin = y + (h - w)/2;    float width   = h;    float height  = w;    CGRect horizontalTwoTableViewFrame = CGRectMake(xOrigin, yOrigin, width, height);        self = [super initWithFrame:horizontalTwoTableViewFrame style:style];    if (self) {        self.transform = CGAffineTransformMakeRotation(-M_PI/2);//这是关键,水平放置的table        /**          *  打印         *  (CGRect) horizontalTwoTableViewFrame = origin=(x=349.5, y=-349.5) size=(width=325, height=1024)         *  <HorizontalTwoTableView: 0x79358a00; baseClass = UITableView; frame = (0 0; 1024 325); transform = [0, -1, 1, 0, 0, 0];         **/        //        self.lastScrolledIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];        self.lastHighlightedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];        //    }    return self;}


#import "HorizontalTwoTableViewCell.h"#define kHorizontalTwoTableViewCellRotatedViewTag 200@interface HorizontalTwoTableViewCell ()@property (nonatomic, retain)AdvertisementEntity *contentEntity;@property (nonatomic, retain)UIImageView         *selectedImageView;@property (nonatomic, retain)DynamicImageView    *theImageView;@end
// cell  <span style="font-family: Arial, Helvetica, sans-serif;">原创 http://blog.csdn.net/u014558625/</span>@implementation HorizontalTwoTableViewCell-(void)dealloc{    self.contentEntity = nil;        self.selectedImageView = nil;    self.theImageView = nil;        [super dealloc];}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self)    {        // Initialization code                self.accessoryType = UITableViewCellAccessoryNone;        self.selectionStyle = UITableViewCellSelectionStyleNone;        self.backgroundColor = [UIColor clearColor];        self.contentView.backgroundColor = [UIColor clearColor];                UIView *rotatedView          = [[[UIView alloc] init] autorelease];//(该视图将被旋转M_PI/2)        rotatedView.backgroundColor = [UIColor clearColor];                rotatedView.tag = kHorizontalTwoTableViewCellRotatedViewTag;        [self.contentView addSubview:rotatedView];                                _selectedImageView = [[UIImageView alloc] init];        _selectedImageView.hidden = YES;        [rotatedView addSubview:_selectedImageView];                        _theImageView = [[DynamicImageView alloc] init];        _theImageView.backgroundColor = [UIColor clearColor];        [rotatedView addSubview:_theImageView];                        //一定要调用 setCellContentFrame来初始化视图frame    }    return self;}- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{    [super setHighlighted:highlighted animated:animated];        if (highlighted)    {        [self darkenImageWithUIGestureRecognizerState:UIGestureRecognizerStateChanged];    }    else    {        [self darkenImageWithUIGestureRecognizerState:UIGestureRecognizerStateEnded];    }        DDLogVerbose(@"%@ %@", THIS_FILE,THIS_METHOD);}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];            DDLogVerbose(@"%@ %@", THIS_FILE,THIS_METHOD);}#pragma mark -#pragma mark - designated//适配 iphone6+ 6 5S 4, 对应屏幕尺寸5.5  4.7  4  3.5- (void)setCellContentFrame:(CGRect)cellContentFrame cellPadding:(CGFloat)cellPaddingFloat;{    //cellContentFrame是水平放置的table中cell的自定义frame,cellPaddingFloat是单元格上下的空隙        //因为 使用这个cell的table被旋转-M_PI/2 变成水平放置的table,    //所以 这个单元格也是水平放置的,单元格内容也同时变成水平的;    //但是 在initWithStyle方法中,单元格的内容都被addSubview到rotatedView(该视图将被旋转M_PI/2,此时还没有旋转)    //所以 单元格还是水平放置的,而单元格的内容恢复成纵向的。        //所以 cellContentFrame是水平放置的table中cell的自定义frame,            // 4寸屏幕的情况    // 假设 cellContentFrame = CGRectMake(0, 0, 370, 250)    cellPaddingFloat = 8.0    // 那么 CGRect rotatedViewRect = CGRectMake(68, -68+8, 234.0, 370.0);        // 计算过程如下    // 水平放置的单元格,宽370,高250;  内容视图contentView,宽370,高234;  250-8*2=234;(水平单元格的上下空隙各8)    // xOrigin:(370-234)/2=68  yOrigin:(234-370)/2=-68(然后 -68+8,8是水平单元格的上面空隙);    // 旋转M_PI/2, width,height 互换, 所以 rotatedViewRect 就是 (xOrigin, yOrigin, height, width) = (68, -68+8, 234.0, 370)        // 还记得吗? 单元格的内容恢复成纵向.    // selectedImageView.frame = CGRectMake(0, 0, 234, 370);    // imageButton.frame = CGRectMake(8, 8, 234-16, 370-8-30);  //30预留给label    // titleLabel.frame = CGRectMake(0, 370-30+6, 234, 30-6-8);            float width   = cellContentFrame.size.width;    float height  = cellContentFrame.size.height - cellPaddingFloat*2;    float xOrigin = cellContentFrame.origin.x + (width - height)/2;    float yOrigin = cellContentFrame.origin.y + (height - width)/2 + cellPaddingFloat;        UIView *rotatedView = (UIView *)[self.contentView viewWithTag:kHorizontalTwoTableViewCellRotatedViewTag];    rotatedView.frame = CGRectMake(xOrigin, yOrigin, height, width);    rotatedView.transform = CGAffineTransformMakeRotation(M_PI/2); //这个很关键,与使用这个cell的table旋转方向相反,旋转角度大小相同        // 还记得吗? 单元格的内容恢复成纵向.    float contentWidth  = height;    float contentHeight = width;    _selectedImageView.frame = CGRectMake(0, 0, contentWidth, contentHeight);    _theImageView.frame = CGRectMake(0, 0, contentWidth, contentHeight);}- (void)setCellContentFrame:(CGRect)cellContentFrame{    [self setCellContentFrame:cellContentFrame cellPadding:4.0f];}#pragma mark -#pragma mark -  setters-(void)setShowSelectImageView:(BOOL)show{    _showSelectImageView = show;    if (_showSelectImageView)    {        _selectedImageView.hidden = NO;    }    else    {        _selectedImageView.hidden = YES;    }}#pragma mark -#pragma mark - 自定义- (void)darkenImageWithUIGestureRecognizerState:(UIGestureRecognizerState)aState{//使图片变暗    switch (aState)    {        case UIGestureRecognizerStateBegan: // object pressed        case UIGestureRecognizerStateChanged:            _theImageView.layer.backgroundColor = [UIColor blackColor].CGColor;            _theImageView.layer.opacity = 0.4;            break;                    case UIGestureRecognizerStateEnded: // object released            _theImageView.layer.backgroundColor = [UIColor clearColor].CGColor;            _theImageView.layer.opacity = 1.0;            break;                    default: // unknown tap            DDLogVerbose(@"longPressGR.state %i", aState);            break;    }}- (UIImage *)imageWithColor:(UIColor *)color{    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return theImage;}#pragma mark - #pragma mark - dataSource- (void)updateDataSource:(AdvertisementEntity *)aADEntity{    //清除缓存    _theImageView.image = nil;        //更新数据源    self.contentEntity = aADEntity;    if ( ! self.contentEntity) {        // DDLogVerbose(@"updateDataSource: 没有 数据啊!");        return;    }                    //TODO: test code    _theImageView.image = [UIImage imageNamed:@"iphone"];    //TODO: test code                    //绑定数据    if ([self.contentEntity.adImageURL length])    {        [_theImageView updateImageModuleType:MODULE_TYPE_TASK Url:self.contentEntity.adImageURL withUid:self.contentEntity.adSID withDefaultImage:nil];    }    /**    if ([self.contentEntity.thumbLocalPath length])    {        NSString *filePath = [SystemUtils imageMideaFilePathForFileName:self.contentEntity.thumbLocalPath];        [_imageButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];    }    else if([self.contentEntity.thumbServerUrl length])    {        [self requestSmallImageForContentID:self.contentEntity.contentID URL:self.contentEntity.thumbServerUrl];    }    **/}/**//描述:请求小图片//参数:aContentID内容ID,imageURL图片路径- (void)requestSmallImageForContentID:(NSString *)aContentID URL:(NSString *)imageURL{    dispatch_async(kBgQueue,    ^{        NSDictionary *dic = [[BaseContentListManager sharedContentListModuleManager]                             requestBlockSmallImageForContentID:aContentID                             URL:imageURL];                NSData *imageData = [dic objectForKey:@"ImageData"];        if(imageData)        {            dispatch_sync(dispatch_get_main_queue(),            ^{                                //显示小图                DDLogVerbose(@"%@ %@ 显示小图",THIS_FILE,THIS_METHOD);                if([self.contentEntity.contentID isEqualToString:[dic objectForKey:@"contentID"]])                {                    self.imageView.image = [UIImage imageWithData:imageData];                }                                            });        }            });}**/@end


其实有个更简单的:
首先:
   tableView.transform = CGAffineTransformMakeRotation(-M_PI/2);//这是关键,水平放置的table
然后:
   cell.contentView.transform = CGAffineTransformMakeRotation(M_PI/2);//这是关键




//转载 http://stackoverflow.com/questions/10404116/uitableview-infinite-scrolling

'UITableView' is same as 'UIScrollView' in 'scrollViewDidScroll' method.

So, its easy to emulate infinite scrolling.

  1. double the array so that head and tail are joined together to emulate circular table

  2. use my following code to make user switch between 1st part of doubled table and 2nd part of doubled table when they tend to reach the start or the end of the table.

:

/* To emulate infinite scrolling...The table data was doubled to join the head and tail: (suppose table had 1,2,3,4)1 2 3 4|1 2 3 4 (actual data doubled)---------------1 2 3 4 5 6 7 8 (visualising joined table in eight parts)When the user scrolls backwards to 1/8th of the joined table, user is actually at the 1/4th of actual data, so we scroll instantly (we take user) to the 5/8th of the joined table where the cells are exactly the same.Similarly, when user scrolls to 6/8th of the table, we will scroll back to 2/8th where the cells are same. (I'm using 6/8th when 7/8th sound more logical because 6/8th is good for small tables.)In simple words, when user reaches 1/4th of the first half of table, we scroll to 1/4th of the second half, when he reaches 2/4th of the second half of table, we scroll to the 2/4 of first half. This is done simply by subtracting OR adding half the length of the new/joined table. Written and posted by Anup Kattel. Feel free to use this code. Please keep these comments if you don't mind.*/-(void)scrollViewDidScroll:(UIScrollView *)scrollView_ {      CGFloat currentOffsetX = scrollView_.contentOffset.x;    CGFloat currentOffSetY = scrollView_.contentOffset.y;    CGFloat contentHeight = scrollView_.contentSize.height;    if (currentOffSetY < (contentHeight / 8.0)) {    scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));    }   if (currentOffSetY > ((contentHeight * 6)/ 8.0)) {       scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));    }}

0 0
原创粉丝点击