Add shadow to UIScrollView
scroll1.layer.shadowColor = [UIColor blackColor].CGColor; scroll1.layer.shadowRadius = slider.value; scroll1.layer.shadowOpacity = slider.value; scroll1.layer.masksToBounds = NO; // This is important
scroll1.layer.masksToBounds = NO; // This is important
UIScrollView实现非全屏分页的小技巧
浏览一系列图片时,我们会经常使用UIScrollView来进行实现,因为其自带的pagingEnabled属性,可以非常方便的自动支持手势左右轻扫切换切换页面。不过最近做的一个产品,设计的效果有所不同,希望每一页图片的宽度小于屏幕宽度,但也要保证图片之间是相连的,即在屏幕的中间部分显示一幅图片,中间两边显 示相邻图片的一小部分,最开始发现有点棘手,需要关闭pagingEnabled属性,自己实现分页效果。但是尝试之后发现要想达到满意的交互体验,要做 的事情真不少,偶然的机会想到了一个巧妙的方法来达到这一效果,即:
将UIScrollView大小设置为一张图片的大小,然后嵌入一个满屏的UIView中,设置clipsToBounds属性为NO,则UIScrollView中的图片内容会溢出frame所限制的区域,从而巧妙地实现了不全屏分页的效果:)
yourScrollView.clipsToBounds = YES
Adding Properties to an Objective-C Category
Let’s say you have a category that needs to store some information. Unfortunately you can’t add an instance variable, but you can add something called an associated reference. From the documentation:
Associative references, available starting in Mac OS X v10.6, simulate the addition of object instance variables to an existing class
To create an association you use the objc_setAssociatedObject
function and to retrieve an association use the objc_getAssociatedObject
method.
@interface UIView (DHStyleManager) @property (nonatomic, copy) NSString* styleName; @end
#import "UIViewDHStyleManager.h" NSString * const kDHStyleKey = @"kDHStyleKey"; @implementation UIView (DHStyleManager) - (void)setStyleName:(NSString *)styleName { objc_setAssociatedObject(self, kDHStyleKey, styleName, OBJC_ASSOCIATION_COPY); } - (NSString*)styleName { return objc_getAssociatedObject(self, kDHStyleKey); } @end
When instances of UIView are released they will also release their associated references.
This technique let’s use set this custom property on plain old UIViews. If you only need to do something simple like add a property, this provides a nice alternative to subclassing.
#import UIViewDHStyleManager.h" UIView* v = [[[UIView alloc] init] autorelease]; v.styleName = @"someStyleName"; NSLog(@"v = %@",v.styleName); //Logs 'someStyleName'
关于设置UIView的背景为图片的方法以及问题
一、加一个uiimageview在uiview上面
UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.bounds];
imageView.image = [[UIImage imageNamed:@"name.png"] stretchableImageWithLeftCapWidth:left topCapHeight:top]; [view addSubview:imageView];
这种方式,如果原始图片大小不够(小于view的大小),可以拉伸,在view释放后也没有什么内存保留。
二、通过图片来生成UIColor设置view的backgroundColor
1.imageNamed方式
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"name.png"]];
2.contentOfFile方式
NSString* path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"]; view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path];
这两种方式都会在生成color时占用大量的内存(原始图片的n倍,这个n可能会达到几千的程度)。而且如果图片大小不够,就会按照原始大小一个一个u画 过去,也就是不会自动拉伸。1和2的区别在于,view释放后,1中的color并不会跟着释放,而是一直存在于内存中(当然,再次根据这个图片生成 color时并不会再次申请内存了),而2中的color就会随着view的释放而释放。
三、quartzCore方式
UIImage *image = [UIImage imageNamed:@"name.png"]; view.layer.contents = (id) image.CGImage; // 如果需要背景透明加上下面这句 view.layer.backgroundColor = [UIColor clearColor].CGColor;
这种方式会自动拉伸图片,而且没有额外内存占用。
综上,推荐第三种方式来根据图片设置背景色。