您的位置:首页 > 其它

图片以及图片阴影移动

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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐