图片以及图片阴影移动
2015-03-01 09:17
148 查看
今天来学习一个比较好玩的东西,图片的移动以及阴影的移动.游戏规则:当拖动图形移动时,图像的阴影随之发生变化,图片停止移动时,阴影消失.初学者可以看看.
第一步:创建两个类,一个类继承于UIView,一个继承于UIViewController.
第二步:
view.h
建两个坐标的属性
@property (assign,nonatomic)
CGPoint point;
@property (assign,nonatomic)
CGPoint point1;
view.m
- (void)dealloc{
[superdealloc];
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
}
return
self;
}
// 重写父类的方法
//开始移动时,阴影的偏移量为0,意味着图片没有阴影.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s", __func__);
UITouch *bTouch = [touches
anyObject];
self.point1 = [bTouchlocationInView:self];
self.layer.shadowOffset =CGSizeMake(0,0);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s", __func__);
//获得手指的点击事件
UITouch *aTouch = [touches
anyObject];
//根据触摸对象,来获得相对于视图的坐标
self.point = [aTouchlocationInView:self.superview];
NSLog(@"point.x = %f,point.y = %f",_point.x,_point.y);
self.layer.shadowOffset =CGSizeMake(
- _point.x/5.0,50 -
_point.y/2.0);
[selfsetFrame:CGRectMake(_point.x-self.point1.x,_point.y
- self.point1.y,self.frame.size.width,self.frame.size.height)];
self.layer.shadowRadius = self.point.y/50
- 5;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//移动停止,阴影的偏移量为0,没有阴影.
self.layer.shadowOffset =CGSizeMake(0,0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
第三步:
viewController.m
新建一个View的对象.
- (void) conViewFirst{
conView *aView = [[conViewalloc]
initWithFrame:CGRectMake(50,100,
200, 100)];
aView.backgroundColor = [UIColorcyanColor];
//
设置阴影
aView.layer.shadowColor = [UIColordarkGrayColor].CGColor;
//
阴影的偏移量
aView.layer.shadowOffset =CGSizeMake(10,10);
//
阴影的透明度
aView.layer.shadowOpacity =0.5;
[self.viewaddSubview:aView];
[aViewrelease];
}
第四步:
AppDelegate.m
self.window = [[UIWindowalloc]
initWithFrame:[[UIScreenmainScreen]
bounds]];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
[_windowrelease];
conViewController *con = [[conViewControlleralloc]
init];
[_windowsetRootViewController:con];
[conrelease];
第一步:创建两个类,一个类继承于UIView,一个继承于UIViewController.
第二步:
view.h
建两个坐标的属性
@property (assign,nonatomic)
CGPoint point;
@property (assign,nonatomic)
CGPoint point1;
view.m
- (void)dealloc{
[superdealloc];
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
}
return
self;
}
// 重写父类的方法
//开始移动时,阴影的偏移量为0,意味着图片没有阴影.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s", __func__);
UITouch *bTouch = [touches
anyObject];
self.point1 = [bTouchlocationInView:self];
self.layer.shadowOffset =CGSizeMake(0,0);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s", __func__);
//获得手指的点击事件
UITouch *aTouch = [touches
anyObject];
//根据触摸对象,来获得相对于视图的坐标
self.point = [aTouchlocationInView:self.superview];
NSLog(@"point.x = %f,point.y = %f",_point.x,_point.y);
self.layer.shadowOffset =CGSizeMake(
- _point.x/5.0,50 -
_point.y/2.0);
[selfsetFrame:CGRectMake(_point.x-self.point1.x,_point.y
- self.point1.y,self.frame.size.width,self.frame.size.height)];
self.layer.shadowRadius = self.point.y/50
- 5;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//移动停止,阴影的偏移量为0,没有阴影.
self.layer.shadowOffset =CGSizeMake(0,0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
第三步:
viewController.m
新建一个View的对象.
- (void) conViewFirst{
conView *aView = [[conViewalloc]
initWithFrame:CGRectMake(50,100,
200, 100)];
aView.backgroundColor = [UIColorcyanColor];
//
设置阴影
aView.layer.shadowColor = [UIColordarkGrayColor].CGColor;
//
阴影的偏移量
aView.layer.shadowOffset =CGSizeMake(10,10);
//
阴影的透明度
aView.layer.shadowOpacity =0.5;
[self.viewaddSubview:aView];
[aViewrelease];
}
第四步:
AppDelegate.m
self.window = [[UIWindowalloc]
initWithFrame:[[UIScreenmainScreen]
bounds]];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
[_windowrelease];
conViewController *con = [[conViewControlleralloc]
init];
[_windowsetRootViewController:con];
[conrelease];
相关文章推荐
- 用相对定位和负向移动完成图片象框阴影
- 使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果
- RecyclerView实现瀑布流遇到的各种问题(item移动,加载更多图片闪烁,以及定制各种类型Header和Footer)
- Js实现图片轮播,以及鼠标移动选择指定图片
- HTML鼠标移动到图片上时显示阴影边框
- 鼠标在文本上移动时层的显示与消失(以及层里显示图片)ASP.NET
- 图片使用鼠标移动和滚轮缩放js以及插件
- 图片算法以及窗口阴影
- iOS 图片查看关闭,以及放大缩小旋转移动
- 图片跟随鼠标移动(期间鼠标不释放)以及释放鼠标(图片)时刻鼠标位置
- C#实现图片阴影以及发光
- 鼠标移动到图片上,图片显示阴影边框,其他一些样式积累
- [原创] JavaScript 图片放大镜插件 enlarge.js 以及移动版 enlarge.touch.js
- 55-002-1 HTML5图片列表页面的制作 鼠标移动图片动态显示,文字动态阴影效果,渐变效果 列表前增加样式序列号
- Android手势缩放图片以及图片黏贴在手指随手势移动
- 使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果
- andriod 利用WebView实现 图片放大缩小以及移动[转]
- MFC 图片控件图片加载以及鼠标移动事件和鼠标点击事件
- css3实现小米商城鼠标移动图片上浮阴影效果
- Android手势缩放图片以及图片黏贴在手指随手势移动